1编写第一个程序(python 3)
print('hello world!')
2.查看python 版本号
python -v
3.使用变量
message = 'hello world!'
print(message)
4.字符串
name = 'jetty'
print(name.title()) #Jetty 首字母大写
print(name) jetty
name.upper() #JEETY 转大写
name.lower() #jetty 转小写
5.合并拼接字符串
first_name = 'hongzhu'
last_name = 'zhan'
full_name = last_name +" "+ first_name
print(full_name) zhan hongzhu
6.使用制表来添加空白
language = 'python\nJavascript\nC\nRust'
print(language)
打印
python
Javascript
C
Rust
7.删除空白
_blank = ' python '
print(_blank.rstrip()) #去除右侧空白
print(_blank.strip()) #去除两侧空白
print(_blank.lstrip()) #去除左侧空白
8.变量类型
num = 2.340
print(int(num)) #整型 2
print(float(num)) #浮点型 2.34
9.列表
color = ['red','green','yellow','pink']
#访问元素
print(color[0]) ##red
#修改
color[0] = 'black'
#添加元素
color.append('orange')
#插入元素
color.insert(0,'blue') #插到第一位
print(color)
#删除元素
del color[0] #删除当前元素
color.pop() #删除数组最后一个元素
color.remove('red') #删除红色
10 组织列表
10.1排序列表
num_list = [1,2,3,4,2,1,3,1,2]
num_list.sort()
print(num_list) [1, 1, 1, 2, 2, 2, 3, 3, 4]
10.2临时排序
num_list = [1,2,3,4,2,1,3,1,2]
print(sorted(num_list)) [1, 1, 1, 2, 2, 2, 3, 3, 4]
10.3reverse 反序
num_list = [1,2,3,4,2,1,3,1,2]
num_list.reverse()
print(num_list) [2, 1, 3, 1, 2, 4, 3, 2, 1]
10.4列表的长度
num_list = [1,2,3,4,2,1,3,1,2]
print(len(num_list)) 9
11 遍历列表
num_list = [1,2,3,4,2,1,3,1,2]
for i in num_list:
print(i,end=" ") #一行显示
12.使用函数遍历
num_list = [1,2,3,4,2,1,3,1,2]
for i in range(len(num_list)):
print(num_list[i],end=" ")
13.乘方运算
squares = []
for i in range(1,6):
squares.append(i**2)
print(squares) [1, 4, 9, 16, 25]
14.内置函数
num_list = [1,2,3,4,2,1,3,1,2]
print(max(num_list)) 4
print(min(num_list)) 1
print(sum(num_list)) 19
15.列表解析
squeres = [value**2 for value in range(1,11)]
print(squeres) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
16.练习示例
16.1 1-50奇数的和
odd_number =[]
for i in range(1,11,2):
odd_number.append(i)
print(sum(odd_number))
16.2 3-90 3的倍数
three_nmu = []
for i in range(3, 91):
if (i % 3==0):
three_nmu.append(i)
print(three_nmu)
16.3 1-10 的立方
squares = []
for i in range(3,11):
squares.append(i**3)
print(squares)
16.4 1-10 的立方列表解析
squares = [i**3 for i in range(3,11)]
print(squares)
17 列表切片
num_list = [1,2,3,4,2,1,3,1,2]
print(num_list[0:5]) [1, 2, 3, 4, 2] #从第一个开始取值到第五位
print(num_list[:5]) [1, 2, 3, 4, 2] #默认会从第一个开始取值
print(num_list[5:]) [1, 3, 1, 2] #取后面的4位
18 元组
dimensions = (100,300)
print(dimensions[0]) 100
for i in dimensions:
print(i) 100 300
19 if 语句
num_list = [1, 2, 3, 4, 2, 1, 3, 1, 2]
for i in num_list:
if i == 2:
print(i)
20 !=
num_list = [1, 2, 3, 4, 2, 1, 3, 1, 2]
for i in num_list:
if i != 2:
print(i)
21 and
num_list = [1, 2, 3, 4, 2, 1, 3, 1, 2]
for i in num_list:
if i >=1 and i <=2:
print(i)
22 字典
alien = {'color':0,'points':1}
print(alien['color']) ##color
23 修改字典
alien = {'color':0,'points':1}
alien['color'] = 'red'
print(alien) {'color': 'red', 'points': 1}
24 删除字典
alien = {'color':0,'points':1}
del alien['color']
print(alien)
25 案例练习
25.1创建两个人的字典,存储在列表,遍历列表,输出列表
people_nums1 = {'name':'jetty','name1':'jack'}
people_nums2 ={'name':'kitty','name1':'james'}
peoples = [people_nums1,people_nums2]
for i in peoples:
print(i)
26.用户输入和while循环
ipt = input('你是小黄么?1(true) or 2(false)?')
if ipt =='1':
print('是本人')
else:
print('不是本人')
27 % //运算符
print(4 % 2) 0
print(4 // 2) 2
28 while运算符
count = 0
arr = []
while count < 20:
for j in range(1, 100):
if j % 11 == 0:
count = count+1
arr.append(j)
print(arr)
29 函数
#简单求和
def num_sum(arr):
result =0
for i in arr:
result =result+i
return result
print(num_sum([1,2,3,4])) 10
30 函数默认值
def num_sum(arr=[1,2,3]):
result =0
for i in arr:
result =result+i
return result
print(num_sum()) 6
31 传递任意数量的实参
def make_prize(*top):
return top
print(make_prize(1))
print(make_prize(1,2,3))
print(make_prize(1,3,4,5))
print(make_prize(1,1,1,1,1))
#返回
(1,)
(1, 2, 3)
(1, 3, 4, 5)
(1, 1, 1, 1, 1)
32 导入函数
# 随机数
import random
print(random.randint(1,19))