Python自学笔记001

最近在看莫烦的Python教程,留一些笔记方便以后回看。

print字符串

  • 字符串要加""或者''
  • 对于字符串本身含有'的,可以用转义字符,如:
>>> print('I\'m zxy')
I'm zxy
  • 字符串叠加使用+:
>>> print('abc'+'def')
abcdef

运算

>>> print(1+1)
2
>>> print(2**3) # **为Python中平方的表示方法
8
>>> print(int('3')+5)
8
>>> print(int(3.5))
3
>>> print(float(3.2)+8)
11.2
>>> 8%3 # 取余的方法
2

变量赋值

变量命名规则

  • 变量名只能包含字母、数字和下划线。变量名可以字母或下划线开头,但不能以数字开头。
  • 变量名不能包含空格,但可使用下划线来分隔其中的单词。

赋值数字

>>> apple=1
>>> print(apple)
1

>>> a,n,m=1,2,3 # 一次赋值多个变量
>>> print(a,n,m)
1 2 3

赋值字符串

>>> apple='hjjjhh'
>>> print(apple)
hjjjhh

while循环

使用while来表达循环:

a=1
while a <= 10: # 注意while条件后面必须有冒号
    print(a)
    a = a + 1 # Python中不能使用a++实现自增,只能使用a=a+1或a+=1

输出如下:

1
2
3
4
5
6
7
8

for循环

# 使用for循环遍历example_list
example_list = [1,2,3,4,5,6,7,8,222,333]
for i in example_list: #同样注意此处的冒号
    print(i)

输出如下:

1
2
3
4
5
6
7
8
222
333

你可能感兴趣的:(python)