Python基础学习1——变量、循环、判断语句等

一、变量

1、变量的定义

变量名只能是 字母、数字或下划线的任意组合

变量名的第一个字符不能是数字

以下关键字不能声明为变量名

['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']

二、注释

单行注释  #后均为注释内容,且为了保证可读性,#后添加一个空格,直接在代码后注释的时候,#前空两个格

多行注释 连续的三对引号(单引号双引号都可以),多行注释可以当多行字符串

# -*-coding utf-8-*- 放在第一行,声明使用的是utf8字符编码

三、格式化输出

1、利用+

举例:

1 name = “hui”
2 
3 str = '''
4 
5 my name is '''+name+''',hahahh
6 '''

2、利用占位符

1 name = "hui"
2 
3 str = '''
4 
5 my name is %s,hahahh
6 
7 '''%(name)

3、利用{} 常用

name = "hui"

str = '''

my name is {_name},hahahh

'''.format(_name=name)

四、if语句

1 if x == 1 :
2     print(x)
3 else :
4     print(x-1)

或者:

1 if x > 90 :
2     print("优秀")
3 elif x >60 and x<=90 :
4     print("良好")
5 else :
6     print("不及格")

五、while语句

while True :
    print("123")

六、for语句

for i in range(10)
    print(i)

猜你喜欢

转载自www.cnblogs.com/hui0623/p/11108208.html