Print("hello word!")#打印hello word!向python世界发生第一声呐喊,仪式很重要
定义变量
name="Alex Li"
name2=name
print("my name is ",name,name2)
输出结果:my name is Alex Li Alex Li
name="paoche ge"
print(name,name2)
输出结果:Alex Li paoche ge #nam2通过name找到Alex的内存地址,name重新赋值,会开辟另一个内存地址存paoche ge,name2还是alex
变量赋值不能有空格,第一个字符必须是(字母或_,可有字母,_和数字组成,大小写敏感)
PIE=3.1415296 #可以用大写字母表示常量
input
1 name=input("name:") 2 age=int(input("age:")) #interger 3 job=input("job:") 4 salary=input("salary:")
格式输出
1 1 info=""""------info of {_name}----- 2 2 Name={_name} 3 3 Age={_age} 4 4 Job={_job} 5 5 Salary={_salary} 6 6 ---------end-------""" 7 7 .format(_name=name, 8 8 _age=age, 9 9 _job=job, 10 10 _salary=salary) 11 11 print(info) 12 12 13 13 info2= 14 14 """------info of %s----- 15 15 Name=%s 16 16 Age=%d 17 17 Job=%s 18 18 Salary=%S 19 19 ---------end-------"""%(name,name,age,job,salary) 20 20 Print(info2) 21 21 22 22 info3 = 23 23 =""""------info of {0}----- 24 24 Name={0} 25 25 Age={1} 26 26 Job={2} 27 27 Salary={3} 28 28 ---------end-------""".fomat(name,name,age,job,salary) 29 29 print(info3)
while循环
1 1 age_of_oldboy=56 2 2 count=0 3 3 while count<3: #判断循环条件 4 4 guess_age = int(input("guess age:")) # input默认读取字符串 5 5 if guess_age==age_of_oldboy: 6 6 print("yes,good!") 7 7 break 8 8 elif guess_age > age_of_oldboy: 9 9 print("think smaller") 10 10 else: 11 11 print("think bigger") 12 12 count +=1 13 13 if count==3: 14 14 print("fuck off") 15 """也可以不用elif 16 17 age_of_oldboy=56 18 19 count=0 20 21 while count<3: #判断循环条件 22 23 guess_age = int(input("guess age:")) # input默认读取字符串 24 25 if guess_age==age_of_oldboy: 26 27 print("yes,good!") 28 29 break 30 31 if guess_age > age_of_oldboy: 32 33 print("think smaller") 34 35 if guess_age < age_of_oldboy: 36 37 print("think bigger") 38 39 count +=1 40 41 else: 42 43 print("fuck off") 44 45 """
guess任性玩
1 1 age_of_oldboy=56 2 2 count=0 3 3 while count<3: #判断循环条件 4 4 guess_age = int(input("guess age:")) # input默认读取字符串 5 5 if guess_age==age_of_oldboy: 6 6 print("yes,good!") 7 7 break 8 8 elif guess_age > age_of_oldboy: 9 9 print("think smaller") 10 10 else: 11 11 print("think bigger") 12 12 count +=1 13 13 if count==3: 14 14 print("are you want to try again?") 15 15 continue_result=input("") 16 16 if continue_result==("no")or continue_result==("n"): 17 17 count==4 18 18 else: 19 19 count=0
for循环
1 for i in range(10): 2 print("------",i) 3 for j in range(10): 4 print(j) 5 if j>5: 6 break #跳出当前整个循环//continue 是跳出本次循环继续下次循环
for循环之猜年龄
1 age_of_oldboy=56 2 i=0 3 for i in range (3): #判断循环条件 4 guess_age = int(input("guess age:")) # input默认读取字符串 5 if guess_age==age_of_oldboy: 6 print("yes,good!") 7 break 8 elif guess_age > age_of_oldboy: 9 print("think smaller") 10 else: 11 print("think bigger") 12 i+=1 13 if i==3: 14 print("fuck off") 15 16 for i in range(0,10,3): #从0开始到10结束,每隔三个打印一个 17 print("loop",i) 18 for i in range(10): 19 print("------",i) 20 for j in range(10): 21 print(j) 22 if j>5: 23 break #跳出当前整个循环//continue 是跳出本次循环继续下次循环
时间: 2024-10-25 18:04:06