格式化输出的三种方式
一.占位符
程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式
比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age is xxx.
很明显,用逗号进行字符串拼接,只能把用户输入的名字和年龄放到末尾,无法放到指定的xxx位置,而且数字也必须经过str(数字)的转换才能与字符串进行拼接,非常之麻烦,我们来试一试。
age = 19
print('My name is xxx,my age is '+str(age))
My name is xxx,my age is 19
age = 19
print('My name is xxx,my age is', age)
My name is xxx,my age is 19
name = 'nick'
age = 19
print('My name is '+name+' my age is '+str(age))
My name is nick my age is 19
上面使用的方法越看越别扭,越看越麻烦。这就需要用到占位符,如:%s(针对所有数据类型)、%d(仅仅针对数字类型)
name = 'nick'
age = 19
print('my name is %s my age is %s' % (name, age))
my name is nick my age is 19
age = 19
print('my age is %d' % age)
my age is 19
二、format格式化
name = 'nick'
age = 19
print("Hello, {}. You are {}.".format(name, age))
Hello, nick. You are 19.
name = 'nick'
age = 19
print("Hello, {1}. You are {0}-{0}.".format(age, name))
Hello, nick. You are 19-19.
name = 'nick'
age = 19
print("Hello, {name}. You are {age}-{age}.".format(age=age, name=name))
Hello, nick. You are 19-19.
三、f-String格式化
name = "nick"
age = 19
print(f"Hello, {name}. You are {age}.")
Hello, nick. You are 19.
大写的F也适用。
name = "nick"
age = 19
print(F"Hello, {name}. You are {age}.")
Hello, nick. You are 19.
age = 19
print(f'{age*2}')
38
再给你秀个以后可能会用到的操作。
salary = 6.6666
print(f'{salary:.2f}')
6.67
基本运算符
当我们眼前飘过一只生物后,我们会立即获得这个生物的信息,种类、性别、身高、三维,当我们获取这些信息的同时,我们还会马上对这些信息做一些逻辑处理,如这个生物种类是老虎的时候,我们会跑开;这个生物是人,性别为女,但是身高只有一米三时,我们可能会不自觉地靠近?
一、算术运算符
print(1+2)
3
x = 10
y = 20
res = x+y
print(res)
30
# /有零有整除,得到一个浮点型
print(10/3)
3.3333333333333335
# 地板除,只取整数部分
print(10//3)
print(10//4)
3
2
# %:取余
print(10 % 3)
1
# **,幂
print(10**3)
1000
二、比较运算符
pwd = '123'
print(pwd != '123')
print(pwd == '123')
False
True
l1 = [1, 'a', 3]
l2 = [3]
print(l1 < l2) # False
True
try:
l3 = [1, 3]
print(l1 < l3) # 报错,列表比较大小仅限于同一位置的对应的值是相同的类型
except Exception as e:
print(e)
name 'l1' is not defined
三、赋值运算符
age = 19
age = age + 1
print(age)
20
age = 19
age += 1
print(age)
20
age = 19
age *= 10
print(age)
190
四、逻辑运算符
# 从左到右的方式找到逻辑运算符,找到逻辑运算符的左边,左边成立,再去找到逻辑运算符的右边
print(3 > 3 and 1 > 2 or 2 > 1) # False
True
五、身份运算符
is和==的区别:is用于判断两个变量引用对象是否为同一个(是否在同一块内存空间中), ==用于判断引用变量的值是否相等。
x = 257
y = x
z = 257
print(f'x is y:{x is y}')
print(f'x == y:{x == y}')
print(f'x is z:{x is z}')
print(f'x == z:{x == z}')
x is y:True
x == y:True
x is z:False
x == z:True
六、Python运算符优先级
# Python中True为1,False为0
print(True > 0) # True
print(True > 2) # Flase
流程控制之if判断
一.语法
if判断是干什么的呢?if判断其实是在模拟人做判断。就是说如果这样干什么,如果那样干什么。对于ATM系统而言,则需要判断你的账号密码的正确性。
1.1 if
if 条件:
代码1
代码2
代码3
...
# 代码块(同一缩进级别的代码,例如代码1、代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行)
cls = 'human'
gender = 'female'
age = 18
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('开始表白')
print('end...')
开始表白
end...
1.2 if...else
if...else表示if成立代码成立会干什么,else不成立会干什么。
cls = 'human'
gender = 'female'
age = 38
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('开始表白')
else:
print('阿姨好')
阿姨好
1.3 if...elif...else
if...elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,elif...否则干什么。
cls = 'human'
gender = 'female'
age = 28
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('开始表白')
elif cls == 'human' and gender == 'female' and age > 22 and age < 30:
print('考虑下')
else:
print('阿姨好')
考虑下
二、if的嵌套
# if的嵌套
cls = 'human'
gender = 'female'
age = 18
is_success = False
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('开始表白')
if is_success:
print('那我们一起走吧...')
else:
print('我逗你玩呢')
else:
print('阿姨好')
开始表白
我逗你玩呢
三、练习
# 成绩评判
score = input("your score: ")
score = int(score)
if score >= 90:
print('优秀')
# elif score >= 80 and score < 90:
elif score >= 80:
print('良好')
# elif score >= 70 and score < 80:
elif score >= 70:
print('普通')
else:
print('差')
your score: 80
良好
3.2 练习2:模拟登录注册
username: nick
password: 123
username or password error