Python基础(一)
1、Python注释:有以下三种方式
# print('hello')
'''
print('hello')
print('hello')
'''
"""
print('hello')
print('hello')
"""
2、变量
my_name = 'TOM'
print(my_name)
3、bug与Debug
略
4、数据类型
- 数值:int、float
- 布尔型:True、False
- str
- list
- tuple
- set
- dict
num1 = 1
num2 = 1.0
bool1 = True
str1 = 'hello'
print(type(num1))
print(type(num2))
print(type(bool1))
print(type(str1))
list1 = [1, 2]
tuple1 = (1, 2)
set1 = {1, 2}
dict1 = {'name': 'TOM', 'age': 23}
print(type(list1))
print(type(tuple1))
print(type(set1))
print(type(dict1))
5、格式化输出
age = 23
name = 'TOM'
weight = 60.2
print('My name is %s.' % name, end="\t")
print('My name is %s, my age is %d, my weight is %.2f.' % (name, age, weight), end="\n")
print(f'My name is {name}, my age is {age}, my weight is {weight}', end="\n")
6、输入
input接收的数据类型都是字符串。
pswd = input('Input Password >> ')
print(f'Password:{pswd}')
print(type(pswd))
7、数据类型转换
num = input('Input Number >> ')
print(f'Number:{num}, type:{type(num)}')
print(f'Number:{num}, type:{type(int(num))}')
num1 = 1
str1 = '10'
print(type(float(num1)))
print(float(num1))
print(type(float(str1)))
print(float(str1))
list1 = [1, 2]
print(tuple(list1))
tuple1 = (1, 2)
print(list(tuple1))
# eval()的使用
str2 = '1'
str3 = '1.1'
str4 = '[1, 2]'
str5 = '(1, 2)'
print(type(eval(str2)))
print(type(eval(str3)))
print(type(eval(str4)))
print(type(eval(str5)))
8、运算符
print(1 + 2)
print(1 - 2)
print(1 * 2)
print(1 / 2)
print(1 // 2)
print(1 % 2)
print(1 ** 2)
多变量赋值
num1, float1, str1 = 1, 2.2, 'str'
print(f'num1:{num1}, float1:{float1}, str1:{str1}')
a = b = c = 10
print(f'a:{a}, b:{b}, c:{c}')
逻辑运算符:and/or/not
print((1 == 1) and (1 == 2))
print((1 == 1) or (1 == 2))
print(not (1 == 1))
数字之间的逻辑运算
- and:只要有一个数字为0,则为0,否则结果为最后一个非0数字
- or:只有所有值为0,才为0,否则结果为第一个非0数字