python 循环变量_python 变量以及循环

print(“hello world”)

变量 : 存储信息的,日后被调用、修改操作

常量: 固定不变的量,字母大写

命名规则:

1. 字母数字下划线组成

2. 不能以数字开头,不能含有特殊字符和空格

3. 不能以保留字命名

4. 不能以中文命名

5. 定义的变量名应该有意义

6. 驼峰式命、 下划线分割单词

7. 变量名区分大小写

a=1

b=2

if a

print("Yes")

print("Yes")

print("Yes")

print("Yes")

else:

print("No")

a=1

b=2

if a>b:

print("Yes")

elif a==b:

print("第三")

else:

print("any")

if 条件1:

自拍

elif 条件2:

else:

跳舞

# 单行注释

'''多行注释'''

""" 多行注释 """

input()

"abc" + "qwe"

file.py

文件的扩展名:

.py : python的程序文件

.txt : 文本文件

pdf chm html doc xml xls ppt

jpg png gif jpeg bmp

avi rmvb MP4 mkv 3gp

WMV MP3 flue mid

True 真 正确的

False 假 错误的

a

b = 100

c = 1000

if b <= a <= c:

print("True")

num number

num1 = intpu("Num1:")

num2 = intpu("Num2:")

num3 = intpu("Num3:")

输出三个数字中的最大值/最小值

if num1>num2>num3:

#num1最大

else:

#num1

if num1>num2>num3 #num1最大

elif num1>num3>num2 #num1最大

elif num2>num1>num3 #num2最大

elif num2>num3>num1 #num2最大

elif num3>num2>num1 #num3最大

else

num3>num1>num2 #num3最大

num1 num2 num3

max_num =0

if num1>num2:

max_num= num1

if max_num > num3:

print("Max NUM is",max_num)

else:

print("Max NUM is",num3)

else:

max_num = num2

if max_num > num3:

print("Max NUM is",max_num)

else:

print("Max NUM is",num3)

num += 1 等价于 num = num + 1

num -= 1 等价于 num = num - 1

num *= 2 等价于 num = num * 2

num /= 2 等价于 num = num / 2

num //= 2 等价于 num = num // 2

num %= 2 等价于 num = num % 2

num **= 2 等价于 num = num ** 2

and 且,并且

只有两个条件全部为True(正确)的时候, 结果才会为True(正确)

条件1 and 条件2

5>3 and 6<2 True

or 或,或者

只要有一个条件为True,则结果为Ture,

5>3 or 6<2

真 或 假

not

not 5>3 == False

not 5<3 == True

a>b and (c>d or (not f))

(not (not True)) or (False and (not True))

条件1 and 条件2

条件1 or 条件2

短路原则

对于and 如果前面的第一个条件为假,那么这个and前后两个条件组成的表达式 的计算结果就一定为假,第二个条件就不会被计算

对于or

如果前面的第一个条件为真,那么这个or前后两个条件组成的表达式 的计算结果就一定为真,第二个条件就不会被计算

True or True and False

猜年龄

age = 50

user_input_age = int(input("Age is :"))

if ....

while 循环

while 条件:

print("any")

print("any")

num = 1

while num<10: # 2

print(num) # 2

num+=1 # 3

if num == 9: # 3

break

num = 1

while num<=100: # num<=100 等价于 True

# while num<=100: 等价于 while True:

if num%2 == 0:

print(num)

num += 1

num = 1

while num<=100:

if num%2 == 1:

print(num)

num += 1

age = 50

#user_input_age = int(input("Age is :"))

flag = True

while flag:

user_input_age = int(input("Age is :"))

if user_input_age == age:

print("Yes")

flag =False

elif user_input_age > age:

print("Is bigger")

else:

print("Is smaller")

print("End")

break # 终止

age = 50

#user_input_age = int(input("Age is :"))

#flag = True

# break

while True:

user_input_age = int(input("Age is :"))

if user_input_age == age:

print("Yes")

break

elif user_input_age > age:

print("Is bigger")

else:

print("Is smaller")

print("End")

continue 继续

if a>b and d3 and d == e:

......

while 条件:

....

else:

....

statement 语句

num = 1

while num <= 10:

num += 1

if num == 5:

break

print(num)

else:

print("This is else statement")

while 条件1:

.....

while 条件2:

....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值