栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python入门学习

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python入门学习

#注:在python中需要注意代码之间的缩进,通常以一个tab的距离表示隶属关系

import os 
import math,copy,random,time
from collections import Counter 
import numpy as np  

def hello_world():  
	#创建函数格式:def name(参数1,参数2....):
	yourname = input('你好,请输入你的名字:')  
	print('欢迎来到Python的世界,',yourname)  
	print('让我们开始学习吧~')

def hello_twice():
	global yourname,yourheight,yourweight  
	yourname = input('请输入你的名字:')
	yourheight = input('请输入你的身高:')
	yourweight = input('请输入你的体重:')

#python中字符串的部分操作
def deviding_line():
	word1 = 'i am line'  
	word2 = word1.upper()  
	word3 = word1.lower()  
	word4 = word1.title()  
	#以上三个函数仅为字符串的部分函数
	words = [word1,word2,word3,word4]  
	line = '-' * 40  

	endReturn = line+words[random.randint(0,3)]+line 
	
	return endReturn  

#学习python中的数字模型
def study_number():
	num1 = input('请输入一个数字:')
	print('你输入的是数字%s'%num1,'可它的类型为:',type(num1)) 
	#18、type()函数可以返回该值的类型
	num2 = int(input('再输入一个数字:'))  
	print('你输入的是数字%s' % num2, '它的类型为:', type(num2))
	num3 = float(input('再输入一个数字:'))  
	print('你输入的是数字%s' % num3, '它的类型为:', type(num3))
	print('num1+num2={}'.format(int(num1)+num2)) 
	
	print('num1-num2={}'.format(int(num1)-num2))   
	print('num1*num2={}'.format(num1*num2)) 
	print('num1*num2={}'.format(int(num1) * num2))  
	print('num2//num3={:.3f}'.format(num2//num3))
	print('num2/num3={:.4f}'.format(num2/num3)) 
	print('num2%num3={:.4f}'.format(num2 % num3)) 
	print('num2%num3={:.4%}'.format(num2%num3)) 
	print('num1**num2={}'.format(int(num1)**num2))  
	print('This is the {a},and {b}'.format(a='numbers',b='some operations')) 

	one,two,three = True,True,False  
	print(one,two,three)
	print('and运算符:',one and two,one and three) 
	print('or运算符:',one or two,one or three) 
	print('not运算符:',not one,not two,not three)  

#学习python中的列表模型
def study_list(length):
	l1 = [1,2,3,4,5,9.0]   
	l2 = list(range(10,10+length))  
	
	print('l1的类型为:',type(l1))
	print(l1[1],l2[1])  
	l3 = l2  
	print(id(l1),id(l2),id(l3)) 
	l3[0]=99 
	print('l2==l3么?',l2==l3)   
	l4 = l2.copy()  
	l4[0]=999
	print('l4==l2么?',l4==l2)  
	print('删除前',l4)
	del l4[0]  
	print('删除后',l4)
	l4.append(30)  
	l4.extend(l1)  
	print('添加l1后:',l4)
	l4.reverse()  
	print('反转后:',l4)
	l4.sort()  
	print('排序后:',l4)

#学习python中的元组模型
def study_tuple(length:int)->bool: 
	tuple1 = (1,2,3,4)  
	tuple2 = tuple(range(10,10+length))  

	print(tuple1.count(1))  
	print(tuple1.index(1)) 
	try:   
		tuple1[0] = 9  
	except TypeError:
		print('元组插入失败')
	finally:  
		print('不管插入成不成功我都会执行')

	try:
		print(id(tuple1),id(tuple2))
	except:
		return False
	else:
		tuple3 = tuple1+tuple2  
		print(tuple3,id(tuple3))
	return True

#学习python中的字典模型
def study_dict():  
	dict1 = {1:'一',2:'二',3:'三',4:'四'}  
	dict2 = dict(one=1,two=2,three=3)
	dict3 = dict(zip([6,7,8,9],['Six','Seven','Eight','Nine']))
	dict4 = dict([('One',1),('Two',2),('Three',3)])
	dict5 = dict({1:'一',2:'二',3:'三',4:'四'})
	print(type(dict1),dict1==dict5)  
	print(dict1[1],dict2['one'],dict3[6],dict4['One'],dict5[1])  
	print(dict1.get(4)) 

	dict1[1] = '壹' 
	dict1[5] = '五' 
	print(dict1)
	print(1 in dict1, 6 in dict1, 7 not in dict1) 
	dict6 = dict1.copy()  
	dict6[1] = 'One'
	print(dict1,'',dict6)

	dict1.clear() 
	print(dict1)
	del dict1,dict2,dict3,dict4,dict5,dict6 
    
#python中集合的学习,集合中不存在相等的值
def study_set(): 
	set1 = set(['You','Are','Not','Beautiful']) 
	set2 = {'You','Are','So','Beautiful'} 
	set3 = set2.copy() 

	print(type(set1))
	print(set1,set2)
	print(set1|set2)  
	print(set1&set2)  
	print(set1^set2)  
	print(set1-set2)  
	print(set1<=set2,set3<=set2,set3 
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994879.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号