原视频链接:https://search.bilibili.com/all?keyword=BV1xs411Q799&from_source=nav_search_new
介绍python的重要性,用更少的语句完成更多的任务。
python的安装。
IDLE是一个Python Shell。 shell的意思就是“外壳”,基本上来说,就是一个通过键入文本与程序交互的途径。(跟命令行差不多)
语句 | 结果 |
---|---|
print(“Hello World”) | Hello World |
print “Hello World” | × |
print(5+3) | 8 |
print(“Hello”+“World”) | HelloWorld |
print(“Hello”*8) | HelloHelloHelloHelloHelloHelloHelloHello |
print(“Hello” + 8) | × |
print("-----------------------我爱鱼C工作室----------------------------------")
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字\n")
guess = int(temp)
if guess == 8:
print("猜中了")
else:
print("没猜中")
print("游戏结束")
BIF:Built - in - functions (内置函数)
teacher = "小甲鱼"
print(teacher)
# 小甲鱼
first = 8
second = 3
third = first + second
print(third)
# 11
first = "小"
second = "甲鱼"
third = first + second
print(third)
# 小甲鱼
使用变量之前需要对他进行赋值
变量名可以包括字母、数字、下划线,但是不能以数字开头
字母大小写是区分开来的,相同的拼写不同的大小写组合,结果得到的是两个不同的变量
“ = ” 是赋值的意思,左边是名字,右边是值
转义字符的使用( \ )+ 原始字符串( r ):
print('Let\'s go !')
# Let's go !
str = 'C:\now'
# C:
# ow
str = r'C:\now'
# C:\now
str = 'C:\\now'
# C:\now
print(str)
切忌使用中文标点符号
print("-----------------------我爱鱼C工作室----------------------------------")
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字\n")
guess = int(temp)
if guess == 8:
print("猜中了")
else:
if guess > 8
print("大了")
if guess < 8
print("小了")
print("游戏结束")
循环语法 : while
print("-----------------------我爱鱼C工作室----------------------------------")
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字\n")
guess = int(temp)
while guess != 8:
temp = input("请重新输入\n")
guess = int(temp)
if guess == 8:
print("猜中了")
else:
if guess > 8:
print("大了")
if guess < 8:
print("小了")
print("游戏结束")
布尔值 True False
print(3>2)
# Ture
print(3>2 and 3>4)
# False
random模块,randint() 返回一个随机的整数
import random
secret = random.randint(1,10)
print("-----------------------我爱鱼C工作室----------------------------------")
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字\n")
guess = int(temp)
while guess != secret:
temp = input("请重新输入\n")
guess = int(temp)
if guess == secret:
print("猜中了")
else:
if guess > secret:
print("大了")
if guess < secret:
print("小了")
print("游戏结束")
‘520’ (字符串) 520 (数字)
e记法 (e其实就是10,跟在e后面的数代表10的几次方):
print(15e10)
# 150000000000.0
print(15e-5)
# 0.00015
类型转换
整数:int()
浮点数:float()
字符串:str()
type(), isinstance(): 查看变量类型
a = '520'
print(type(a))
#
print(isinstance(a, str))
# True
print(isinstance(a, int))
# False
a = b = c = d = 10
a += 1
b -= 3
c *= 10
d /= 2
print(a) # 11
print(b) # 7
print(c) # 100
print(d) # 5.0
print(10//8) # 1
print(2**3) # 8
优先级跟正常加减乘除一样
飞机游戏的设计思路
x, y = 4, 5
if x < y:
small = x
else:
small = y
print(small)
# 4
small = x if x < y else y
print(small)
# 4
断言 assert,当这个关键字后面的条件为假的时候,程序自动崩溃并抛出AssertionError异常。
while 循环
i = 0
while i < 5:
print(i)
i += 1
# 0 1 2 3 4
for循环
member = ['qwe', 'qwed', 'edee']
for each in member:
print(each, len(member))
# qwe 3
# qwed 3
# edee 3
a = 'HelloWorld'
for i in a:
print(i, end = '')
# HelloWorld
range()
print(list(range(5)))
# [0, 1, 2, 3, 4]
for i in range(5):
print(i)
# 0 1 2 3 4
for i in range(2,9):
print(i)
# 2 3 4 5 6 7 8
for i in range(1, 10, 2):
print(i)
# 1 3 5 7 9
break, continue
for i in range (10):
if i % 2 != 0:
print(i)
continue
i += 2
print(i)
# 2 1 4 3 6 5 8 7 10 9