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
相关推荐
Arthurmoo23 分钟前
Linux系统之MySQL数据库基础
linux·数据库·mysql
博观而约取28 分钟前
Django ORM 1. 创建模型(Model)
数据库·python·django
李洋-蛟龙腾飞公司41 分钟前
HarmonyOS NEXT应用元服务常见列表操作分组吸顶场景
linux·运维·windows
链上Sniper1 小时前
智能合约状态快照技术:实现 EVM 状态的快速同步与回滚
java·大数据·linux·运维·web3·区块链·智能合约
精灵vector2 小时前
构建专家级SQL Agent交互
python·aigc·ai编程
晨曦丿2 小时前
双11服务器
linux·服务器·网络
Zonda要好好学习2 小时前
Python入门Day2
开发语言·python
Vertira2 小时前
pdf 合并 python实现(已解决)
前端·python·pdf
太凉2 小时前
Python之 sorted() 函数的基本语法
python
李迟2 小时前
在Linux服务器上使用kvm创建虚拟机
java·linux·服务器