python基础(1)--input print if else elif while 用法说明

1 变量名的命名规则:

  由数字,字母和下划线组成,但是不能以数字开头命名变量。例如 a ,b ,c ,name ,user1 user_id 等都可作为变量名称。

  1a,2b 3cd等都不行。特别注意不能以python语法中的关键字作为变量名。常见的有“class” "and" "as" "insert " "while" "elif ""else " " del" 等。

  python中的变量不需要声明,直接使用。

2 input用法

  等待用户输入。

  括号内用双引号引起来在输入前显示的内容

>>> n=input("please input your name:")回车
please input your name:huo
>>> 

3 print 用法

  输出指定信息

1 直接输出英文字符
>>> print("huo") huo
2 直接输出中文字符
>>> print("你好") 你好
3 输出变量所指代的内容(此时不加引号)
>>> n="hello world" >>> print(n) hello world >>>

4 if else 用法

  判断条件是否成立,成立则执行if 下的代码块 否则执行else

a=input("please input a:")
b=input("please input b:")
#if 条件 :回车写代码块注意代码块与 if 是相错开的
if a<b:     #判断a是否小于b
    print(a)#如果a小于b 则执行
else:
    print(b)#否则执行else
    

  运行结果

please input a:2
please input b:4
2
>>> 
please input a:4
please input b:2
2
>>> 

if语句支持嵌套

#找出三个数中的最大一个
a=input() b=input() c=input() t="0" if a<b: t=a a=b b=t if a<c: print(c) else: print(a) else: if a<c: print(c) else: print(a)

  运行结果

================== RESTART: C:/Users/Shinelon/Desktop/1.py ==================
1
3
2
3
>>> 
================== RESTART: C:/Users/Shinelon/Desktop/1.py ==================
4
6
5
6

elif 类似else if

a=input()
b=input()
c=input()
t="0"
if a<b:
    t=a
    a=b
    b=t
    if a<c:
        t=a
        a=c
        c=t
elif a>b:
    if a<c:
        t=a
        a=c
        c=t
if b<c:
    t=b
    b=c
    c=t
print(a)
print(b)
print(c)

while 用法

  循环语句

示例

name=input("please input your name:")
count=0
while count<3:
    passwd=input("please input your password:")
    if passwd=="393407505":
        count=3
        print("登入成功!")
    else:
        print("登入失败")
        count=count+1

运行结果

please input your name:huo
please input your password:11111
登入失败
please input your password:11111
登入失败
please input your password:11111
登入失败
>>> 
================== RESTART: C:/Users/Shinelon/Desktop/1.py ==================
please input your name:huo
please input your password:393407505
登入成功!
>>> 

总结:   多练习 想法更重要!

猜你喜欢

转载自www.cnblogs.com/qingfenghanli/p/9541509.html