python初始

1.变量

a = 12
b = "huihui"

变量:将计算的中间结果存储起来,以便后续代码使用。
变量设定规则:
       1,必须是字母,数字下划线任意组合。
       2,不能是数字开头。
  3,不能是python中的关键字。
       ['and', 'as', 'assert', 'break', 'class', 'continue',
      'def', 'del', 'elif', 'else', 'except', 'exec',
  'finally', 'for', 'from', 'global', 'if', 'import',
  'in', 'is', 'lambda', 'not', 'or', 'pass', 'print',
  'raise', 'return', 'try', 'while', 'with', 'yield']
  4,变量不能是中文。
  5,变量不能太长。
  6,变量有具有可描述性。

  AgeOfOldboy = 56
  NumberOfStudents = 80

  下划线

  age_of_oldboy = 56
  number_of_students = 80

2.input交互

# 将用户输入的内容赋值给 name 变量
name = input("请输入用户名:")
  
# 打印输入的内容
print(name)

3.数据类型

1. 整数类型(int)

>>> b = 2**60
>>> type(b)
<type 'int'>

2. 字符串类型(str)

>>> name = "Alex Li" #双引号
>>> age = "22"       #只要加引号就是字符串
>>> age2 = 22          #int
>>> 
>>> msg = '''My name is Alex, I am 22 years old!'''  #我擦,3个引号也可以
>>> 
>>> hometown = 'ShanDong'   #单引号也可以

3. bool型 (True,false)

>>> a=3
>>> b=5
>>> 
>>> a > b #不成立就是False,即假
False
>>> 
>>> a < b #成立就是True, 即真
True

4.流程控制--if

if 条件:
    满足条件后要执行的代码

双分支
"""
if 条件: 满足条件执行代码 else: if条件不满足就走这段 """ AgeOfOldboy = 48 if AgeOfOldboy > 50 : print("Too old, time to retire..") else: print("还能折腾几年!")

多分支

if 条件:
    满足条件执行代码
elif 条件:
    上面的条件不满足就走这个
elif 条件:
    上面的条件不满足就走这个
elif 条件:
    上面的条件不满足就走这个    
else:
    上面所有的条件不满足就走这段
复制代码

5.循环while

  看见break循环终止

count = 0
while count <= 100 : #只要count<=100就不断执行下面的代码
    print("loop ", count)
    if count == 5:
        break
    count +=1 #每执行一次,就把count+1,要不然就变成死循环啦,因为count一直是0

print("-----out of while loop ------")

输出

loop  0
loop  1
loop  2
loop  3
loop  4
loop  5
-----out of while loop ------

看见 continue 不终止循环  从这层循环跳出 在循环

count = 0
while count <= 100 : 
    count += 1
    if count > 5 and count < 95: #只要count在6-94之间,就不走下面的print语句,直接进入下一次loop
        continue 
    print("loop ", count)

print("-----out of while loop ------")

输出

loop  1
loop  2
loop  3
loop  4
loop  5
loop  95
loop  96
loop  97
loop  98
loop  99
loop  100
loop  101
-----out of while loop ------

-练习题-

1、使用while循环输入 1 2 3 4 5 6     8 9 10
count = 0
while count<10:
    count += 1
    if count == 7:
        continue
    print(count)
2、求1-100的所有数的和
count = 1
sun = 0
while count<101:
    # print(count)
    sun = count + sun
    count = count + 1
print(sun)
3、输出 1-100 内的所有奇数
count = 0
while count < 101:
    if count%2 == 1:
        print(count)
    count += 1                # count = count + 1
4、输出 1-100 内的所有偶数
count = 0
while count < 101:
    if count%2 == 0:
        print(count)
    count += 1                # count = count + 1
5、求1-2+3-4+5 ... 99的所有数的和
'''
给x赋值为0,给y赋值为0,while真,循环开始
如果x和2的余数等于0,那么x就为偶数,y的赋值就等于y减去x
否则x就为奇数,y的赋值就等于y加x
如果x等于99了,break跳出当前循环,x的赋值等于x加1一直循环到x等于99
打印y
'''
x = 0
y = 0
while True:
x += 1
if x % 2 == 0:
y = y - x
else:
y = y + x
if x == 99:
break
print(y)
6、用户登陆(三次机会重试)  (3次后选择继续还是结束)
username="huihui"
password = "123"
count = 0
while count < 3:
    count += 1
    username_=input("请输入你的账号:")
    password_=input("请输入你的密码:")

    if username_ == username and password_ == password:   #用户名和密码和输入的一样
        print("欢迎",username)
        break
    else:
        print("你输入的用户或密码不正确")

        if count == 3:
            chiose = input("你还要在继续吗?继续(r)结束(b):")
            if chiose == "r":
                count = 0
       else:
          break


原文地址:https://www.cnblogs.com/luchenhui/p/8954386.html