- 注释:
# 单行注释
"""
多行注释
"""
- 变量
name = "TOM"
print(name)
schoolName = "黑马"
print(schoolName)
- 数据类型
# int
num1 = 1
print(type(num1))
# float
num2 = 1.1
print(type(num2))
# str
a = 'hello world'
print(type(a))
# bool
b = True
print(type(b))
# list 列表
c = [1, 2, 3]
print(type(c))
# tuple 元组
d = (10, 20, 30)
print(type(d))
# set 集合
e = {1, 2, 3}
print(type(e))
# dict 字典
f = {'name': 'TOM', 'age': 18}
print(type(f))
- 格式化输出
age = 18
name = 'TOM'
weight = 75.5
stu_id = 1
stu_id2 = 1000
# 1.今年我的年龄是x岁
print('今年我的年龄是%d岁' % age)
# 2.我的名字是x
print('我的名字是%s' % name)
# 3.我的体重是x公斤
print('我的体重是%.2f公斤' % weight)
# 4.我的学号是x
print('我的学号是%d' % stu_id)
# 4.1我的学号是001
print('我的学号是%03d' % stu_id)
print('我的学号是%03d' % stu_id2)
# 5.我的名字是x,今年x岁了
print('我的名字是%s,今年%d岁了' % (name, age))
# 5.1我的名字是x,明年x岁了
print('我的名字是%s,今年%d岁了' % (name, age + 1))
# 6.我的名字是x,今年x岁了,体重是x公斤,学号是x
print('我的名字是%s,今年%d岁了,体重是%.2f公斤,学号是%03d' % (name, age, weight, stu_id))
# 语法f'{表达式}'
print(f'我的名字是{name},明年{age + 1}岁了')
- 转义字符
print('hello\npython')
print('\thello')
print('world', end='\t')
print('python', end='...')
- 输入
password = input('请输入您的密码:')
print(f'您输入的密码是{password}')
print(type(password))
- 数据转换
num = '1'
print(type(int(num)))
print(type(float(num)))
num2 = 2
print(type(str(num2)))
list1 = [1, 2, 3]
print(tuple(list1))
tuple1 = (100, 200, 300)
print(list(tuple1))
str1 = '1'
str2 = '1.1'
str3 = '(1000,2000,3000)'
str4 = '[1000,2000,3000]'
print(type(eval(str1)))
print(type(eval(str2)))
print(type(eval(str3)))
print(type(eval(str4)))
- if语法
if 2 > 1:
print('2>1')
age = 20
if age < 18:
print(f'您的年龄{age},童工')
# elif (age >= 18) and (age <= 60):
elif 18 <= age <= 60:
print(f'您的年龄{age},合法')
else:
print(f'您的年龄{age},退休年龄')
print("hello")
- 随机数
import random
num = random.randint(0, 2)
print(num)
- 三目运算符
a = 1
b = 2
c = a if a > b else b
print(c)
- 循环
# while循环
i = 0
while i < 5:
print('hello python')
i += 1
if i == 2:
continue
if i == 2:
break
# for循环
str1 = 'python'
for s in str1:
if s == 't':
continue
if s == 'h':
break
print(s)
# while else
while i > 5:
print(i)
else:
print(f'else->{i}')
# for else
for s in str1:
print(s)
else:
print('for end')
- 字符串
str1 = 'hello'
str2 = "python"
desc = '''
自动化运维
自动化测试
web开发
爬虫
机器学习
数据分析
人工智能
'''
desc2 = """
hello python
"""
print(desc)
print(str1[1])