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

1、变量

一、变量的定义ui

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

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

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

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

2、注释

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

多行注释 连续的三对引号(单引号双引号均可以),多行注释能够当多行字符串utf-8

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

3、格式化输出

一、利用+it

举例:字符编码

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

 

二、利用占位符

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

三、利用{} 经常使用

name = "hui"

str = '''

my name is {_name},hahahh

'''.format(_name=name)

 

4、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("不及格")

 

5、while语句

while True :
    print("123")

 

6、for语句

for i in range(10)
    print(i)
相关文章
相关标签/搜索