python基础部分语法
作者:互联网
1.列表
player=["1","2"]
2.使用切片
print(player[0:1]);//打印第一个元素
for p in player[:1]:
print(p)
3.元组
不可修改的列表
a=(1,2,3)
a[0]=2//erro
4.判断条件and /or
5.if elif else判断
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
if的用法
name =["k"]
if "k" in name:
print("k in here")
6.字典
键值相对应
table={"name":"sneaker","color":"red"}
print(table['name']) //结果为输出sneaker
- 添加
table["name2"]="faker" //name2原本的table中不含有
- 删除
del table['name2'] //删除table中的name2
- 遍历
键值同时:
for a,b in table.item():
print("\na:"+a);
print("b:"+b);
单独遍历键:
for a in table.keys():
print(a)
单独遍历值:
for a in table.values():
print(a)
7.用户输入input
message=input("请输入你要输出的信息")
print(message);
8.while break
continue都与c语言中同理
while 1:
print(i)
i+=1
if i== 4:
break
9.函数
def greet_hello():
print("hello ")
greet_hello();//调用
传入任意数量的实参
def make_pizza(*toppings):
"""打印顾客点的所有配料"""
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
10.函数导入
//pizza.py
def make_pizza(size, *toppings): """概述要制作的比萨"""
print("\nMaking a " + str(size) +"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
//从pizza.py中导入make_pizza函数
//并将起取个别名为mp
from pizza import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')
导入所有的函数
from pizza import *
11.类
weixin_44866921 发布了15 篇原创文章 · 获赞 0 · 访问量 284 私信 关注标签:name,python,make,基础,语法,toppings,print,table,pizza 来源: https://blog.csdn.net/weixin_44866921/article/details/104112745