Python入门教程.docx
《Python入门教程.docx》由会员分享,可在线阅读,更多相关《Python入门教程.docx(72页珍藏版)》请在冰豆网上搜索。
![Python入门教程.docx](https://file1.bdocx.com/fileroot1/2022-12/12/db6b72b2-2038-42ba-a5d0-b1c6954e3a79/db6b72b2-2038-42ba-a5d0-b1c6954e3a791.gif)
Python入门教程
Python入门教程1----PythonSyntax
1Python是一个高效的语言,读和写的操作都是很简单的,就像普通的英语一样
2Python是一个解释执行的语言,我们不需要去编译,我们只要写出代码即可运行
3Python是一个面向对象的语言,在Python里面一切皆对象
4Python是一门很有趣的语言
5变量:
一个变量就是一个单词,只有一个单一的值
练习:
设置一个变量my_variable,值设置为10
[cpp]
#Writeyourcodebelow!
my_variable=10
3第三节
1Python里面有三种数据类型interage,floats,booleans
2Python是一个区分大小写的语言
3练习
1把变量my_int值设置为7
2把变量my_float值设置为1.23
3把变量my_bool值设置为true
[python]
#Setthevariablestothevalueslistedintheinstructions!
my_int=7
my_float=1.23
my_bool=True
6Python的变量可以随时进行覆盖
2练习:
my_int的值从7改为3,并打印出my_int
[python]
#my_intissetto7below.Whatdoyouthink
#willhappenifweresetitto3andprinttheresult?
my_int=7
#Changethevalueofmy_intto3online8!
my_int=3
#Here'ssomecodethatwillprintmy_inttotheconsole:
#Theprintkeywordwillbecoveredindetailsoon!
printmy_int
7Pyhton的声明和英语很像
8Python里面声明利用空格在分开
3练习:
查看以下代码的错误
[python]
defspam():
eggs=12
returneggs
printspam()
9Python中的空格是指正确的缩进
2练习:
改正上一节中的错误
[python]
defspam():
eggs=12
returneggs
printspam()
10Python是一种解释执行的语言,只要你写完即可立即运行
2练习:
设置变量spam的只为True,eggs的值为False
[python]
spam=True
eggs=False
11Python的注释是通过“#”来实现的,并不影响代码的实现
2练习:
给下面的代码加上一行注释
[python]
#thisisacommentsforPython
mysterious_variable=42
12Python的多行注释是通过“"""""" ”来实现的
2练习:
把下面的代码加上多行
[python]
"""
thisisaPythoncourse
"""
a=5
13Python有6种算术运算符+,-,*,/,**(幂),%
2练习:
把变量count_to设置为1+2
[python]
#Setcount_toequalto1plus2online3!
count_to=1+2
printcount_to
14Python里面求x^m,写成x**m
2练习:
利用幂运算,把eggs的值设置为100
[python]
#Seteggsequalto100usingexponentiationonline3!
eggs=10**2
printeggs
1练习:
1写一行注释
2把变量monty设置为True
3把变量python值设置为1.234
4把monty_python的值设置为python的平方
[python]
#thisisaPython
monty=True
python=1.234
monty_python=python**2
Python入门教程2----TipCalculator
1把变量meal的值设置为44.50
[python]
#Assignthevariablemealthevalue44.50online3!
meal=44.50
1把变量tax的值设置为6.75%
[python]
meal=44.50
tax=6.75/100
1设置tip的值为15%
[python]
#You'realmostthere!
Assignthetipvariableonline5.
meal=44.50
tax=0.0675
tip=0.15
1把变量meal的值设置为meal+meal*tax
[python]
#Reassignmealonline7!
meal=44.50
tax=0.0675
tip=0.15
meal=meal+meal*tax
设置变量total的值为meal+meal*tax
[python]
#Assignthevariabletotalonline8!
meal=44.50
tax=0.0675
tip=0.15
meal=meal+meal*tax
total=meal+meal*tip
print("%.2f"%total)
Python入门教程3----StringsandConsoleOutput
15 Python里面还有一种好的数据类型是String
16一个String是通过''或者""包成的串
3设置变量brian值为"Alwayslookonthebrightsideoflife!
"
[python]
#Setthevariablebrianonline3!
brian="Alwayslookonthebrightsideoflife!
"
1练习
1把变量caesar变量设置为Graham
2把变量praline变量设置为john
3把变量viking变量设置为Teresa
[python]
#Assignyourvariablesbelow,eachonitsownline!
caesar="Graham"
praline="John"
viking="Teresa"
#Putyourvariablesabovethisline
printcaesar
printpraline
printviking
17Python是通过\来实现转义字符的
2练习把'Help!
Help!
I'mbeingrepressed!
'中的I'm中的'进行转义
[python]
#Thestringbelowisbroken.Fixitusingtheescapebackslash!
'Help!
Help!
\'\mbeingrepressed!
'
18我们可以使用""来避免转义字符的出现
2练习:
把变量fifth_letter设置为MONTY的第五个字符
[python]
"""
Thestring"PYTHON"hassixcharacters,
numbered0to5,asshownbelow:
+---+---+---+---+---+---+
|P|Y|T|H|O|N|
+---+---+---+---+---+---+
0 1 2 3 4 5
Soifyouwanted"Y",youcouldjusttype
"PYTHON"[1](alwaysstartcountingfrom0!
)
"""
fifth_letter="MONTY"[4]
printfifth_letter
19介绍String的第一种方法,len()求字符串的长度
2练习:
把变量parrot的值设置为"NorweigianBlue",然后打印parrot的长度
[python]
parrot="NorwegianBlue"
printlen(parrot)
20介绍String的第二种方法,lower()把所有的大写字母转化为小写字母
2练习:
把parrot中的大写字母转换为小写字母并打印
[python]
parrot="NorwegianBlue"
printparrot.lower()
21介绍String的第三种方法,upper()把所有的大写字母转化为小写字母
2练习:
把parrot中的小写字母转换为大写字母并打印
[python]
parrot="norwegianblue"
printparrot.upper()
第八节
1介绍String的第四种方法,str()把非字符串转化为字符串,比如str
(2)是把2转化为字符串"2"
2练习:
设置一个变量pi值为3.14,把pi转化为字符串
[python]
"""Declareandassignyourvariableonline4,
thencallyourmethodonline5!
"""
pi=3.14
printstr(pi)
22主要介绍“.”的用处,比如上面的四个String的四个方法都是用到了点
2练习:
利用“.”来使用String的变量ministry的函数len()和upper(),并打印出
[python]
ministry="TheMinistryofSillyWalks"
printlen(ministry)
printministry.upper()
23介绍print的作用
2练习:
利用print输出字符串"MontyPython"
[python]
"""TellPythontoprint"MontyPython"
totheconsoleonline4!
"""
print"MontyPython"
1介绍print来打印出一个变量
2练习:
把变量the_machine_goes值赋值"Ping!
",然后打印出
[python]
"""Assignthestring"Ping!
"to
thevariablethe_machine_goeson
line5,thenprintitoutonline6!
"""
the_machine_goes="Ping!
"
printthe_machine_goes
24介绍我们可以使用+来连接两个String
2练习:
利用+把三个字符串"Spam"和"and"和"eggs"连接起来输出
[python]
#Printtheconcatenationof"Spamandeggs"online3!
print"Spam"+"and"+"eggs"
25介绍了str()的作用是把一个数字转化为字符串
2练习:
利用str()函数把3.14转化为字符串并输出
[python]
#Turn3.14intoastringonline3!
print"Thevalueofpiisaround"+str(3.14)
第十四节
1介绍了字符串的格式化,使用%来格式化,字符串是%s
2举例:
有两个字符串,利用格式化%s来输出
[python]
string_1="Camelot"
string_2="place"
print"Let'snotgoto%s.'Tisasilly%s."%(string_1,string_2)
1回顾之前的内容
2练习
1设置变量my_string的值
2打印出变量的长度
3利用upper()函数并且打印变量值
[python]
#Writeyourcodebelow,startingonline3!
my_string="chenguolin"
printlen(my_string)
printmy_string.upper()
Python入门教程4----DateandTime
26介绍得到当前的时间datetime.now()
2练习
1设置变量now的值为datetime.now()
2打印now的值
[python]
18px">fromdatetimeimportdatetime
now=datetime.now()
printnow
27介绍从datetime.now得到的信息中提取出year,month等
2练习:
从datetime.now中得到的信息中提取出year,month,day
[python]
18px">fromdatetimeimportdatetime
now=datetime.now()
printnow.month
printnow.day
printnow.year
28介绍把输出日期的格式转化为mm//dd//yyyy,我们利用的是+来转化
2练习:
打印当前的日期的格式为mm//dd//yyyy
[python]
18px">fromdatetimeimportdatetime
now=datetime.now()
printstr(now.month)+"/"+str(now.day)+"/"+str(now.year)
29介绍把输出的时间格式化为hh:
mm:
ss
2练习:
打印当前的时间的格式为hh:
mm:
ss
[python]
18px">fromdatetimeimportdatetime
now=datetime.now()
printstr(now.hour)+":
"+str(now.minute)+":
"+str(now.second)
第五节
1练习:
把日期和时间两个连接起来输出
[python]
18px">fromdatetimeimportdatetime
now=datetime.now()
printstr(now.month)+"/"+str(now.day)+"/"+str(now.year)+""\
+str(now.hour)+":
"+str(now.minute)+":
"+str(now.second)
Python入门教程5----Conditionals&ControlFlow
30介绍Python利用有6种比较的方式==,!
=,>,>=,<,<=
2比较后的结果是True或者是False
3练习
1把bool_one的值设置为17<118%100
2把bool_two的值设置为100==33*3+1
3把bool_two的值设置为19<=2**4
4把bool_four的值设置为-22>=-18
5把bool_five的值设置为99!
=98+1
[python]
#AssignTrueorFalseasappropriateonthelinesbelow!
bool_one=17<118%100
bool_two=100==33*3+1
bool_three=19<=2**4
bool_four=-22>=-18
bool_five=99!
=98+1
31介绍了比较的两边不只是数值,也可以是两个表达式
2练习
1把bool_one的值设置为20+-10*2>10%3%2
2把bool_two的值设置为(10+17)**2==3**6
3把bool_two的值设置为1**2**3<=-(-(-1))
4把bool_four的值设置为40/20*4>=-4**2
5把bool_five的值设置为100**0.5!
=6+4
[python]
#AssignTrueorFalseasappropriateonthelinesbelow!
bool_one=20+-10*2>10%3%2
bool_two=(10+17)**2==3**6
bool_three=1**2**3<=-(-(-1))
bool_four=40/20*4>=-4**2
bool_five=100**0.5!
=6+4
32介绍了Python里面还有一种数据类型是booleans,值为True或者是False
2练习:
根据题目的意思来设置右边的表达式
[python]
#Createcomparativestatementsasappropriateonthelinesbelow!
#Makemetrue!
bool_one=1<=2
#Makemefalse!
bool_two=1>2
#Makemetrue!
bool_three=1!
=2
#Makemefalse!
bool_four=2>2
#Makemetrue!
bool_five=4<5
33介绍了第一种连接符and的使用,只有and的两边都是True那么结果才能为True
2练习
1设置变量bool_one的值为FalseandFalse
2设置变量bool_two的值为-(-(-(-2)))==-2and4>=16**0.5
3设置变量bool_three的值为19%4!
=300/10/10andFalse
4设置变量bool_four的值为-(1**2)<2**0and10%10<=20-10*2
5设置变量bool_five的值为TrueandTrue
[python]
bool_one=FalseandFalse
bool_two=-(-(-(-2)))==-2and4>=16**0.5
bool_three=19%4!
=300/10/10andFalse
bool_four=-(1**2)<2**0and10%10<=20-10*2
bool_five=TrueandTrue
34介绍了第二种连接符or的使用,只要or的两边有一个True那么结果才能为True
2练习
1设置变量bool_one的值为2**3==108%100or'Cleese'=='KingArthur'
2设置变量bool_two的值为TrueorFalse
3设置变量bool_three的值为100**0.5>=50orFalse
4设置变量bool_four的值为TrueorTrue
5设置变量bool_five的值为1**100==100**1or3*2*1!
=3+2+1
[python]
bool_one=2**3==108%100or'Cleese'=='KingArthur'
bool_two=TrueorFalse
bool_three=100**0.5>=50orFalse
bool_four=TrueorTrue
bool_five=1**100==100**1or3*2