python 入门基础知识练习
1编写第一个程序,目前使用的都是python 3
2.查看当前python编辑器的版本号
3.使用变量
4.字符串
name = 'jetty'
print(name.title())
5.合并拼接字符串
first_name = 'hongzhu'
last_name = 'zhan'
full_name = last_name +" "+ first_name
print(full_name)
6.使用制表来添加空白
language = 'python
Javascript
C
Rust'
print(language)
7.删除空白
_blank = ' python '
print(_blank.rstrip())
8.变量类型
num = 2.340
print(int(num))
9.列表
color = ['red','green','yellow','pink']
10 组织列表
排序列表
num_list = [1,2,3,4,2,1,3,1,2]
num_list.sort()
print(num_list)
临时排序
num_list = [1,2,3,4,2,1,3,1,2]
print(sorted(num_list))
reverse 反序
num_list = [1,2,3,4,2,1,3,1,2]
num_list.reverse()
print(num_list)
列表的长度
num_list = [1,2,3,4,2,1,3,1,2]
print(len(num_list))
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)
14.内置函数
num_list = [1,2,3,4,2,1,3,1,2]
print(max(num_list))
15.列表解析
squeres = [value**2 for value in range(1,11)]
print(squeres)
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])
18 元组
dimensions = (100,300)
print(dimensions[0])
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'])
23 修改字典
alien = {'color':0,'points':1}
alien['color'] = 'red'
print(alien)
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)
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 函数
30 函数默认值
def num_sum(arr=[1,2,3]):
result =0
for i in arr:
result =result+i
return result
print(num_sum())
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))
32 导入函数
33 类
class Dog():
def __init__(self,name,age):
self.name =name
self.age = age
def sit(self):
print(self.name+''+self.age)
dog = Dog('jeety',24)
print(dog.name)
34 类 汽车里程表
class Car():
def __init__(self,make,model,year):
self.make = make
self.model = model
self.year = year
def getCarName(self):
print(self.model)
car = Car('audi','ad4',2016)
print(car.make)
35 子类方法 __init__()
class Car():
def __init__(self,name):
self.name = name
class Elastic(Car):
def __init__(self, name):
super().__init__(name)
myTesla = Elastic('tesla')
print(myTesla.name)
36 class实例
class Car():
def __init__(self,make,name,color):
self.make = make
self.name = name
self.color = color
def getCarName(self):
print('获取车的名字为'+self.name+'获取汽车的颜色'+self.color)
class Batery():
def __init__(self,batery='60'):
self.batery = batery
def discribe_batery(self):
print('This car has'+str(self.batery)+'batery')
class Elatrity(Batery):
def __init__(self, batery):
super().__init__(batery)
self.batery = Batery()
elatrity = Elatrity('100')
print(elatrity.discribe_batery())
37 文件和异常
f = open('file.txt',mode="w",encoding='utf-8')
print(f)
f.write('叫我詹躲躲
')
f.write('叫我詹躲躲1
')
f.close()
38 存储数据
将数据存入json文件
import json
numbers = [1,2,23,3,4,5,6,7,87]
filename = 'numbers.json'
with open(filename,'w') as f_obj:
json.dump(numbers,f_obj)
39 保存和读取用户生成的数据
import json
username = input('存储输入的数据')
filename = 'numbers.json'
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
40 读取用户输入的信息
import json
filename = 'numbers.json'
with open(filename) as f_obj:
username = json.load(f_obj)
print('Welcome back',username)
41 输入和合并数据
import json
filename = 'numbers.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
username = input('存储输入的数据')
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
else:
print('Welcome back',username)
42 封装成为一个函数
import json
def get_username():
filename = 'numbers.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def get_greeting():
username = get_username()
if username:
print('Welcome back',username)
else:
username = input('存储输入的数据')
filename = 'numbers.json'
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
print('Welcome back',username)
get_greeting()