文章目录
本笔记基于p1-p29【莫烦Python】Python 基础教程
大家可以根据代码内容和注释进行学习。
- 安装
我的:python3.8+anaconda+VS code - print()
print(1) # 直接打印数字
print("hello") # 单双引号打印字符串
print('I\'m happy!') # \使符号直接输出
print('apple'+'pen') # 连接字符串
print('chat'+str(4)) # 连接字符串和数字
print(1+2) # 自动计算
print(int('1')+2) # 字符串转整数
print(float('1.2')+2) # 字符串转浮点数
- 数学
1+1
2**2 # 2的平方
2**3 # 2的三次方
8%3 # 取余数
9//4 # 取整(向下)
- 自变量variable
apple_fruit = 1 # 定义和初始化自变量
print(apple_fruit)
apple_egg = 15+3 # 初始化为运算结果
print(apple_egg)
a,b = 1,2 # 一次性定义
c = a*2
print(a,b,c) # 一次性输出
- while循环
conndition = 1
while conndition < 10: # 当while等于true时往下执行,否则结束
print(conndition)
conndition = conndition + 1
- for循环
example_list = [1,2,3,4,5,996,345,67,0] # 定义和初始化自变量
for i in example_list:
print(i) # 循环输出list
print(("inner of for")) # for内部
# 多行Tab:Ctrl+[
print("outer of for") # for外部,说明python中语言结构非常重要
for i in range(2,10): # range(起始数, 终点数+1)
print(i) # 输出2到9
for i in range(2,10,2): # range(起始数, 终点数+1, 步长)
print(i) # 输出2 4 6 8
- if条件
x = 1
y = 2
z = 0
if x < y > z: # 当if为true时执行语句,否则结束
print("x is less than y, and y is greater than z")
if x <= y:
print("x is less or equal to y")
if x == y:
print("x is equal to y") # 无输出
if x != y:
print("x is not equal to y")
- if else条件
x = 1
y = 2
z = 3