学习所看的视频来源于B站,授课老师:Mosh Hamedani老师
视频网址:python教程2019版 6小时完全入门 并且达到能开发网站的能力 目前最好的python教程 (含中文翻译)_哔哩哔哩_bilibili
执行Python代码
打开pycharm编辑器
新建一个Python?file
命名文件
编程
print("Mosh Hamedani")
print('o----')
print(' ||||')
运行:编辑完后Ctrl+'s'保存,然后右击鼠标run这个文件
变量:整数、浮点、字符串、布尔值
第一个程序
name=input('what is your name? ')
print('Hi '+name)
?第二个程序
字符串不能和整数、浮点数直接运算
birth_year=input('birth year: ')
age = 2019 - birth_year
print(age)
转换变量类型
整数? ? ? int()
浮点型? float()
布尔值? bool()
1.
birth_year=input('birth year: ')
age = 2019 - int(birth_year)
print(age)
birth year: 2001
18
2.
weight_kg=input('weight(kg): ')
weight_lbs=int(weight_kg)/0.45
print(weight_lbs)
weight(kg): 46
102.22222222222221
3.引号
单引号、双引号
course1="python's course for beginners"
course2='python for "beginners'
print(course1)
print(course2)
python's course for beginners
python for "beginners
三引号
course='''
Hi John,
here is our first email to you.
thank you,
the support team
'''
print(course)
Hi John,
here is our first email to you.
thank you,
the support team
4.取值
course='python for beginners'
print(course[0])#正取值
print(course[-1])#逆取值
print(course[0:3])
print(course[0:8:2])#隔空取值
print(course[8:0:-1])#逆向隔空取值
print(course[:])#拷贝
print(course[0:])
print(course[0:len(course)])
p
s
pyt
pto
of nohty
python for beginners
python for beginners
python for beginners
5.?
first="john"
last="smith"
massage=first+' ['+last+'] is a corder'
msg=f'{first} [{last}] is a corder'
print(massage)
print(msg)
john [smith] is a corder
john [smith] is a corder
6.大小写
course="Python for Beginners"
print(course.upper())
print(course.lower())
print(course.title())
PYTHON FOR BEGINNERS
python for beginners
Python For Beginners
find()
course="Python for Beginners"
print(course.find('P')) #0
print(course.find('o')) #4
print(course.find('0')) #-1
print(course.find('Beginners')) #11
replace()
print(course.replace('Beginners','Absolute Beginners')) #Python for Absolute Beginners
#字符串中是否包含‘Python’?
print('Python'in course) #True
基础运算
print(10+3) #13
print(10-3) #7
print(10*3) #30
print(10**3) #1000
print(10/3) #3.3333333333333335
print(10//3) #3
print(10%3) #1
数学函数
四舍五入 round()? ? ? ? ? ? ? 绝对值abs()? ? ? ? ? ? ? ? ? ?.ceil()? ? ? ? ? ? ? ? ? ? .floor()
print(round(2.9)) #3
print(round(2.4)) #2
print(abs(-2.9)) #2.9
import math #导入数字模块
print(math.ceil(2.9)) #3
print(math.floor(2.9)) #2
语句
实现下面文本要求
代码:
is_hot=False
is_cold=False
if is_hot:
print('today is hot day')
print('drink plenty of water')
elif is_cold:
print('today is cold day')
print('wear warm clothes')
else:
print('today is lovely day')
print('Enjoy your day')
?练习
solution
price=1000000
has_good_credit = True
if has_good_credit:
down_payment=price*0.1
else :
down_payment=price*0.2
print(f"down payment is:{down_payment}")
down payment is:100000.0
逻辑运算符? (and? or? not)
练习
solution
has_high_income=True
has_good_credit=True
if has_high_income and has_good_credit:
print("Eligible for loan")
?比较运算符(>? <? >=? <=? ==? !=)
练习1
?solution
temperature=30
if temperature > 30:
print("it's a hot day")
elif temperature < 10:
print("it's a cold day")
else:
print("it's a neither hot or cold day")
练习2
?solution
name=input('name: ')
i=len(name)
if i<3:
print('name must be at least 3 characters')
elif i>50:
print('name can be a maximum of 50 characters')
else:
print('name looks good!')
?
?体重单位 磅 和 千克 的转化器
solution
weight=int(input("weight: "))
unit=input("(L)bs or (K)g: ")
if unit.upper()=="L":
i=weight*0.45
print(f"You are {i} kilos")
elif unit.upper()=="K":
i=weight/0.45
print(f"You are {i} pounds")
else :
print('please input l or k')
效果
?while?循环
简单的例子
i=1
while i<=5:
print('*'*i)
i+=1
print('Done')
*
**
***
****
*****
Done
?猜谜游戏
secret_number=9
guess_count=0
guess_limit=3
while guess_count<guess_limit:
guess=int(input('guess: '))
guess_count+=1
if guess==secret_number:
print('you won!')
break
else:
print('sorry,you failed!')
结果?
?练习
command= ""
started=False
while True:
command=input("> ").lower()
if command == "help":
print('''
start - to start the car
stop - to stop the car
quit - to exit
''')
elif command== 'start':
if started:
print('car is already started!')
else:
started=True
print('Car started...')
elif command == 'stop':
if not started:
print('car is already stopped')
else:
started=False
print('Car stopped...')
elif command == 'quit':
break
else:
print("I don't understand that ...")
> HELP
start - to start the car
stop - to stop the car
quit - to exit
> start
Car started...
> start
car is already started!
> stop
Car stopped...
> stop
car is already stopped
> mac
I don't understand that ...
> quit
Process finished with exit code 0
for?循环
for item in 'Python':
print(item)
for item in [1,2,3]:
print(item)
for item in ['john','mosh','smith']:
print(item)
for item in range(10):
print(item)
for item in range(1,10,2):
print(item)
P
y
t
h
o
n
1
2
3
john
mosh
smith
0
1
2
3
4
5
6
7
8
9
1
3
5
7
9
Process finished with exit code 0
练习
(还没看完视频,下午再接着写 。o 0)
|