首页 > 代码库 > day1: 变量,多行打印,for循环,while循环,break

day1: 变量,多行打印,for循环,while循环,break

print(Hello,world!)
name = cc
name2 = name
name = cuichen
print(name,name2)
name2 = name 只是通过name指向‘cc’,name2并不指向name,所以即使name指向改变,name2并不改变



理解变量在计算机内存中的表示:
a = ‘abc’
当我们运行以上代码时,Python解释器干了两件事,1.在内存中创建了一个‘ABC’字符串,2.在内存中创建了一个名为a的变量,并把他指向‘ABC’




for循环可以直接让代码循环执行执行规定的次数,例子如下:

cc_age = 23
for i in range(3):
    age = int(input(His age is :))
    if age == cc_age:
        print(Congratulation!)
        break
    elif age > cc_age:
        print(Too big!)
    else:
        print(Too small!)
else:
        print(You have too many times Fuck off....)

 

for循环还可以用于迭代:
for i in range(0,10,2):#步长为2
    print(-----------,i) 

 

多行打印,例子如下:
apple = input(apple:)
number = int(input(number:))
print(type(number))
qwe = ‘‘‘
--------qwe---------
apple:%s
number:%d
‘‘‘%(apple,number)
print(qwe)

使用‘‘‘   ‘‘‘ 来实现多行打印

 

while循环,while后面跟条件,当条件满足时则一直循环,例子如下:

cc_age = 23
count = 0 #count 代表计数器
while count <  3:
    age = int(input(His age is :))
    if age == cc_age:
        print(Congratulation!)
        break
    elif age > cc_age:
        print(Too big!)
    else:
        print(Too small!)
    count += 1  #等于count = count+1    计数器
else:
        print(You have too many times Fuck off....)

 

 

break 表示跳出此次循环:

for i in range(10):
    print(-------------------,i)
    for j in range(10):
        print(j)
        if j > 5 :
            break

注意break在循环中只是跳出本次循环,并不是终止程序,例子中的大循环套小循环,break终止的只是小循环

 

 





















































 

 

day1: 变量,多行打印,for循环,while循环,break