python入门基础代码图-python入门基础知识(代码)

import abc as aa #导入模块

from aifc import _read_long as fd

from datetime import *

import json

import unittest

print("Hello World!")#打印Hello World!

i=5 #直接使用变量 无需声明

s="hello"

print(str(i)+"."+s) #函数str将其他类型转成字符串

print(s.title()+" "+s.lower()+" "+s.upper())

print(" hello"+" "+" hello".lstrip()) #lstrip函数删除左边空格 类似还有rstrip strip

print(3/2) #python3里 3/2等于1.5

players=['kobe', 'james', 'harden'] #列表

print(players)

print(players[1]) #类似于数组,包括索引从0开始

players[2]='westbrook'

print(players)

players.append('harden') #在列表末尾添加

print(players)

players.insert(2,'simons') #在列表中插入

print(players)

del players[3] #删除

print(players)

p=players.pop() #类似栈的弹出

q=players.pop()

print(p+" "+q)

d=players.pop(1) #指定位置弹出

print(d)

players.remove('kobe') #删除

print(players)

cars=['bmw', 'audi', 'toyota', 'subaru']

cars.sort() #排序

print(cars)

cars.sort(reverse=True)

print(cars)

print(sorted(cars)) #临时排序

print(cars)

cars.reverse() #倒叙

print(cars)

print(len(cars)) #列表长度

for car in cars: #for循环

print(car)

for a in range(5): #生成0-4的数字

print(a)

numbers=list(range(4)) #函数list将range产生的数字转化为列表

print(numbers)

print(numbers[1:3]) #列表的切片

print(numbers[2:])

copynum=numbers[:] #复制列表要使用切片

print(copynum)

dimensions=(200, 50) #元组 无法直接给元组的元素赋值

print(dimensions[0])

dimensions=(200, 100) #要改变元组元素 要重新定义整个元组

print(dimensions)

print('bmw' in cars) #是否包含在列表中

print('audi' not in cars)

people_0={'sex':'man', 'name':'kobe'} #字典

print(people_0['sex'])

people_1={} #创建空字典

people_1['height']=167 #添加键值对

people_1['weight']=58

print(people_1)

people_1['height']=226

print(people_1)

del people_1['height'] #删除键值对

print(people_1)

for k,v in people_0.items(): #循环遍历

print(k+" "+v)

for k in people_1.keys():

print(k)

man_1={'money':12, "house":2}

man_2={'money':22, "house":4}

man_3={'money':18, "house":0}

men=[man_1, man_2, man_3] #字典列表

for m in men:

print(m['money'],m['house'])

print(m.items())

print(m)

cars={ #字典中存储列表

'type':'bus',

'rate':50,

'people':['kobe','james','simos']

}

for car in cars.items():

print(car)

for p in cars['people']:

print(p)

nba={ #字典中嵌套字典

'thunder':{

'pg':'westbrook',

'sg':'roberson',

'sf':'geoger',

'pf':'antony',

'c':'adams',

},

'76ers':{

'pg':'simons',

'sg':'redick',

'sf':'covington',

'pf':'saric',

'c':'embid',

},

}

n=0

while n<=10:

print(n)

n+=1

n=0

while n<10:

print(n)

n+=2

if n==4:

continue

if n==6:

break

for a in nba.items(): #嵌套字典的遍历

for b in a:

print(b)

#msg=input('please input a message: ') #字符串的输入

#print(msg)

#num=input('please input a number: ') #数字的输入

#num=int(num)

#print(num)

def hello(): #定义和调用函数

print("hello world")

hello()

def pri(msg):

print(msg)

pri('Hello')

def test(a,b):

print(a,b)

test(b='hh',a='aa') #关键字实参

def te(a,b='abc'): #参数默认值

return(a,b)

print(a,b)

te('gg')

te('gg','kk')

def rr(a): #函数无需声明返回值

return a+5

cc=rr(5)

print(cc)

def list(m): #传入列表

for mm in m:

print(mm)

list(men)

list(men[:]) #禁止函数对列表的修改 即传入切片

def abc(*a): #可传入任意数量实参

for k in a:

print(k)

abc(1,2,5,8)

class dog(): #类

"""文档注释"""

def __init__(self,name,sex): #相当于构造器 注意前后是两个下划线

self.name=name #self相当于java的this

self.sex=sex

def fun(self):

print("I an funny")

mydog=dog('kevin','woman')

print(mydog.name+" "+mydog.sex)

mydog.fun()

class bigdog(dog):

def __init__(self,name,sex,age):

super().__init__(name,sex) #调用父类构造器

self.age=age

def fun(self): #重写父类方法

print("I am happy")

mybigdog=bigdog('kevin','woman',28)

print(mybigdog.age)

mybigdog.fun()

with open('abc.txt')as afile: #打开文件

contents=afile.read()

print(contents.strip())

with open('abc.txt')as afile:

for line in afile:

print(line.strip())

with open('abc.txt')as afile:

s=""

for line in afile:

s+=line.strip()

print(s)

with open('pi.txt')as pifile: #写入文件

content=pifile.read().strip()

print(content[:100])

with open('gg.txt','w')as myfile:

myfile.write("I love you")

with open('gg.txt','a')as myfile:

myfile.write(" haha")

try: #异常处理

print(5/0)

except ZeroDivisionError:

print('no you can not do this')

else:

pass

with open('test.json','w')as mytest: #存储数据

json.dump("哈哈哈",mytest)

with open('test.json')as mytest:

str=json.load(mytest)

print(str)

class testme(unittest.TestCase):

def test_fu(self):

sss=te("kk")

self.assertEqual(sss,'kkabc')

unittest.main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值