python基础语法

1.第一个python程序

print(‘Hello World’)

2.变量

在python中变量不需要指定变量类型,直接可以使用,例如:

name = 'Tony_Li'

print(name)

输出结果会是 Tony_Li

3.输入输出语法

输入语法:

name=input()

输出语法:
print(name)

4.if...else语句

if...else语句的具体运用如下:

if 条件:

  满足条件的内容

else:

  不满足条件的内容

对于多条件的if语句:

if 条件:

  满足条件的内容

elif:

  满足条件的内容

....

else:

  最后一个内容

例1:

if username == _username and password == _password:

    print('welcoming!')

else:

    print('invaild username or password')

例2:

    if guess_age==age_of_Tony_Li:

        print('yes,you got it!')

        break

    elif guess_age>age_of_Tony_Li:

        print('think smaller!')

    else:

        print('think bigger!')

5.while语句

while的语法如下:

while 条件:

    内容

关于如何跳出循环:

1.break:跳出整个循环

2.continue:跳出本次循环,继续下一次循环

例:

while count<3:

    guess_age = int(input('guess_age:'))

    if guess_age==age_of_Tony_Li:

        print('yes,you got it!')

        break

    elif guess_age>age_of_Tony_Li:

        print('think smaller!')

    else:

        print('think bigger!')

    count +=1

6.for循环

语法:for i in range():

        内容

跳出循环和while一样

例:

for i in range(0,3,1):

    guess_age = int(input('guess_age:'))

    if guess_age==age_of_Tony_Li:

        print('yes,you got it!')

        break

    elif guess_age>age_of_Tony_Li:

        print('think smaller!')

    else:

        print('think bigger!')

if username == _username and password == _password:
print('welcoming!')
else:
print('invaild username or password')
posted @ 2019-06-08 11:05  Tony__Li  阅读(214)  评论(0编辑  收藏  举报