Python简明教程的学习
Python的第一课
笔记前言:
思考了很久,这是我第一次在CSDN博客上记录自己的学习笔记,算是一种过程,也是一种经历,更是一种追求与梦想。
关于我学习的Python:
我使用的是Python3.6版本,编辑器是PyCharm.
- hello world
写的第一个python代码是’hello world’
(学习了PyCharm中python文件的新建,运行和保存)
print('hello world')
- 引号(单引号、双引号和三引号)
单引号和双引号无差别,都是将引号内的空间内容按原样保留。
三引号: 指定多行字符串,也可以在其中间自由使用单、双引号。
'''这是一段多行字符串。这是它的第一行。
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond.'''
- 格式化方法,format()的使用
age=20
name='Swaroop'
print('{0} was {1} years old when he wrote this book.'.format(name,age))
print("why is {0} playing with that python?")
python中的format()方法所做的事情便是将每个参数值替换成格式所在的位置。
#对于浮点数0.33333,保留小数点后3位
print('{0:.3f}'.format(1/3))
#使用下划线填充文本,并保持文字处于中间位置
#使用( ^ )定义'___hello___'字符串长度为11
print('{0:_^11}'.format('hello'))
python小方法:
python 中的print总是以一个不可见的‘新一行’字符(\n)结尾,因此重复调用print将会在相互独立的一行中分别打印。
为防止打印过程中,出现这一个换行符,可以通过end指定其以空白结尾。
print('a',end='')
print('b'.end='')
输出结果:
ab
- 运算符(举几个不熟悉的运算符)
加(+):两个对象相加
3+5 则输出5;
‘a’+‘b’则输出‘a+b’
乘(*):给出两个数的乘积,或返回字符串重复指定次数后的结果。
2* 3 输出6;
‘la’ * 3输出结果’lalala’
乘方():放回x的y次方
34输出81,(即3 * 3 * 3 * 3)
除(/)和整除(//)
取模(%) - 数值运算与赋值的快捷方式
a=2
a=a*6
同样也可以简化成:
a=2
a*=6
- 表达式
length=5
breadth=6
area=length*breadth
print('Area is ',area)
print('Perimeter is ',2*(length+breadth))
输出结果:
Area is 30
Perimeter is 22
- 控制流 if 语句
number =23
guess=int(print('Enter an integer:'))
if guess==number:
print('Congratulations ,you guess it.')
print('(but you do not win any prizes!)')
elif guess < number :
print('No ,it is a little higher than that')
else:
print('No,it is a little lower than that')
print('Done)'
# 这最后一句语句,将在 if 语句执行完毕后执行。
输出结果:
$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
- 控制流while语句
while语句是循环(Looping)语句的一种,while语句同样可以拥有else子句作为可选选项。
number =23
running=Ture
while running:
guess = int(input('Enter an integer : '))
if guess == number:
print('Congratulations, you guessed it.')
# 这将导致 while 循环中止
running = False
elif guess < number:
print('No, it is a little higher than that.')
else:
print('No, it is a little lower than that.')
else:
print('The while loop is over.')
# 在这里你可以做你想做的任何事
print('Done')
输出结果:
$ python while.py
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
- 控制流 for 循环
for…in 语句是另一种循环语句,他会遍历序列中的每一个项目。
for i in range(1,5):
print (i)
else :
print('The for loop is over')
输出结果:
$ python for.py
1
2
3
4
The for loop is over
- 控制流中的break和continue语句
while True:
s=input('Enter something: ')
if s=='quit'
break
print('Length of the string is ',len(s))
# 注意此处的print的缩进,他是和if 并排的,若if条件不满足,则执行该语句
print('Done')
输出结果:
$ python break.py
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something : use Python!
Length of the string is 11
Enter something : quit
Done
continue语句
while True:
s=input('Enter something : ')
if s=='quit':
break
if len(s)<3:
print('Too small')
continue
print('Input is of sufficient length')
输出结果:
$ python continue.py
Enter something : a
Too small
Enter something : 12
Too small
Enter something : abc
Input is of sufficient length
Enter something : quit