python起步

环境:redhat8 python3.6

交互式python
bash 复制代码
[root@rhel8 ~]# python3
Python 3.6.8 (default, Dec  5 2019, 15:45:45) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> exit()
python文件
bash 复制代码
[root@rhel8 ~]# mkdir day01
[root@rhel8 ~]# cd day01/
[root@rhel8 day01]# vim demo01.py
print("hello world")
print("hello"+" world") #首尾相连
print(100+200)  #没有引号,数字运算
print("hello"+100) #会报错
[root@rhel8 day01]# python3 demo01.py 
hello world
hello world
300
Traceback (most recent call last):
  File "demo01.py", line 4, in <module>
    print("hello"+100) #会报错
TypeError: must be str, not int
[root@rhel8 day01]# cat demo01.py 
print("hello world")
print("hello"+" world") #首尾相连
print(100+200)  #没有引号,数字运算
#print("hello"+100) #会报错
print("100"+"200") #100200
#打印多组数据
print("hao",123,"hello") #hao 123 hello 输出中间有空格
#seq:表示多个元素中的分隔符,默认是空格
print("hao",123,"hello",sep="++") #hao++123++hello 分隔符换成了++
#根据输出结果也会发现,每次print语句都会换行,这是因为end参数
print("hello world",end="!!!")#结束符换成!!!,输出看结果。
#hello world!!!hello world
print("hello world")
[root@rhel8 day01]# python3 demo01.py 
hello world
hello world
300
100200
hao 123 hello
hao++123++hello
hello world!!!hello world
input()函数
python 复制代码
[root@rhel8 day01]# python3
Python 3.6.8 (default, Dec  5 2019, 15:45:45) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input()  #通过input()从键盘获取的值,一定是字符串类型的
你好
'你好'
>>> input("username:")
username:zhangsan
'zhangsan'
>>> user = input("username:")
username:zhangsan
>>> print(user)
zhangsan
>>> print("user")
user
>>> print("hello",user)
hello zhangsan
>>> number = input("number:")
number:10
>>> print(number)
10
>>> number + 5  #input()获取的值都是字符类型,字符串和数字不能参与运算
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int
>>> int(number) + 5  #将number变量的值,转换成int类型,整数类型的俩个值可以相互运算
15
>>> number + "5"  #数字类型转换为字符串,并进行拼接操作
'105'
要求写一个input()接收zhangsan,输出Welcome zhangsan的login.py
python 复制代码
[root@rhel8 day01]# cat login.py 
username =  input("username:")
print("Welcome",username)
[root@rhel8 day01]# python3 login.py 
username:zhangsan
Welcome zhangsan
[root@rhel8 day01]# python3 login.py 
username:lisi
Welcome lisi
指定python解释器
python 复制代码
[root@rhel8 day01]# /usr/bin/python3
Python 3.6.8 (default, Dec  5 2019, 15:45:45) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@rhel8 day01]# cat login.py 
#! /usr/bin/python3  
#指定python3
username =  input("username:")
print("Welcome",username)
[root@rhel8 day01]# chmod +x login.py 
[root@rhel8 day01]# ./login.py 
username:zhangsan
Welcome zhangsan
变量

变量定义

在Python中,每个变量在使用前都必须赋值 ,变量赋值以后 该变量才会被创建

等号(=)用来给变量

= 左边是一个变量名

= 右边是存储在变量中的值

变量名 = 值

变量定义之后,后续就可以直接使用了

python 复制代码
vim demo1.py
account = '3186629509@qq.com'
password = '123456'

print(account)
print(password)

[root@rhel8 mypython]# python3 demo1.py 
3186629509@qq.com
123456
练习1

买包子(变量:单价,数量,总价)

可以用其他变量的计算结果来定义变量,变量定义之后,后续就可以直接使用了

需求:

包子的价格是1.5元/个

买了10个包子

计算付款金额

python 复制代码
[root@rhel8 mypython]# vim demo2.py
price = 1.5
num = 10
total = num * price
print("money is",total)
[root@rhel8 mypython]# python3 demo2.py 
money is 15.0
练习2

买包子进阶

今天老板高兴,总价打9折,请重新计算购买金额

python 复制代码
[root@rhel8 mypython]# vim demo2.py
price = 1.5
num = 10
total = (num * price)
total = total * 0.9
print("money is",total)
[root@rhel8 mypython]# python3 demo2.py 
money is 13.5

提问:

上述代码中,一共定义有几个变量?

三个:price/num/total

total = total * 0.9,是在定义新的变量还是在使用变量?

直接使用之前已经定义的变量

变量名只有在第一次出现才是定义变量

变量名再次出现,不是定义变量,而是直接使用之前定义过的变量

在程序开发中,可以修改之前定义变量中保存的值吗?

可以,变量中存储的值,就是可以变的

算术运算符

|------|-----|--------------------------|
| 运算符 | 描述 | 实例 |
| + | 加 | 10 + 20 = 30 |
| - | 减 | 10 - 20 = -10 |
| * | 乘 | 10 * 20 = 200 |
| / | 除 | 10 / 20 = 0.5 |
| // | 取整除 | 返回除法的整数部分(商) 9 // 2输出结果4 |
| % | 取余数 | 返回除法的余数9 % 2 = 1 |
| ** | 幂 | 又称次方、乘方,2 ** 3 = 8 |

python 复制代码
[root@rhel8 mypython]# python3
Python 3.6.8 (default, Dec  5 2019, 15:45:45) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> 10 - 20
-10
>>> 10 * 20
200
>>> 10 / 20
0.5
>>> 20 // 3
6
>>> 20 %  3
2
>>> 2 ** 3
8
比较运算符

|-----|--------------------------------------------------|
| 运算符 | 描述 |
| == | 检查俩个操作数的值是否相等,如果是,则条件成立,返回True |
| != | 检查俩个操作数的值是否不相等,如果是,则条件成立,返回True 1 != 2 --> True |
| > | 检查左操作数的值是否大于右操作数的值,如果是,则条件成立,返回true |
| < | 检查左操作数的值是否小于右操作数的值,如果是,则条件成立,返回true |
| >= | 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立,返回true |
| <= | 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立,返回true |

python 复制代码
>>> 5 > 3
True
>>> 10 < 15 > 13
True
>>> 10 < 15 and 15 > 3
True
逻辑运算符

|-----|---------|---------------------------------------------------|
| 运算符 | 逻辑表达式 | 描述 |
| and | x and y | 只有x和y的值都为True,才会返回True。否则只要x或者y有一个为False,就返回False |
| or | x or y | 只要x或者y有一个值为True,就返回True。只有x和y的值都为False,才会返回False |
| not | not x | 如果x为True,返回False;如果x为False,返回True |

python 复制代码
>>> 10 > 5 and 5 > 3
True
>>> 10 > 5 or 5 > 30
True
>>> 10 > 50
False
>>> not 10 > 50
True

数据类型

数字

基本的数字类型有:

int: 有符号整数

bool: 布尔值

True: 1;False: 0

float浮点数

python 复制代码
>>> type(5)
<class 'int'>
>>> type(1.0)
<class 'float'>
>>> 5 + 3
8
>>> 5 + 3.2
8.2
>>> True + 1
2
>>> False + 1
1
>>> False * 8
0
>>> hex(200)   函数hex(),可以将十进制数200,转换为16进制数
'0xc8'
>>> oct(200)   函数oct(),可以将十进制数200,转换为8进制数
'0o310'
>>> bin(200)   函数bin(),可以将十进制数200,转换为2进制数
'0b11001000'
字符串

Python中字符串被定义为引号之间的字符集合

python 复制代码
>>> words = """
... abc
... def
... ghj
... """
>>> words
'\nabc\ndef\nghj\n'
>>> print(words)

abc
def
ghj
相关推荐
念念0107几秒前
Flask 博客系统(Flask Blog System)
后端·python·flask
tianyuanwo7 分钟前
pyproject.toml 的历史背景和原理
python·pyproject·setup.py
蒋星熠23 分钟前
中间件架构设计与实践:构建高性能分布式系统的核心基石
开发语言·数据库·分布式·python·中间件·性能优化·硬件工程
ZYMFZ2 小时前
Linux 防火墙 Firewalld
linux·运维·服务器
海天一色y3 小时前
Pycharm(二十一)递归删除文件夹
ide·python·pycharm
奔跑吧 android7 小时前
【linux kernel 常用数据结构和设计模式】【数据结构 2】【通过一个案例属性list、hlist、rbtree、xarray数据结构使用】
linux·数据结构·list·kernel·rbtree·hlist·xarray
乔巴先生248 小时前
LLMCompiler:基于LangGraph的并行化Agent架构高效实现
人工智能·python·langchain·人机交互
NiKo_W8 小时前
Linux 文件系统与基础指令
linux·开发语言·指令
张子夜 iiii9 小时前
实战项目-----Python+OpenCV 实现对视频的椒盐噪声注入与实时平滑还原”
开发语言·python·opencv·计算机视觉
Darkwanderor10 小时前
Linux 的权限详解
linux