【小白程序圆python学习记】字典&while&函数

第六章 字典

alien={"color":"red","points":5}
alien["color"]
'red'
new_points=alien["points"]
print("You just earned "+str(new_points)+" points!")
You just earned 5 points!

增加

alien["x"]=1
alien["y"]=2
alien
{'color': 'red', 'points': 5, 'x': 1, 'y': 2}

删除

del alien["y"]  #永远删除
alien
{'color': 'red', 'points': 5}
people={"名":"lucy","姓":"Amy","年龄":"20","城市":"Los"}
people
{'名': 'lucy', '姓': 'Amy', '年龄': '20', '城市': 'Los'}

遍历

likes={"xiao":"python","wang":"r","liu":"c++"}
likes
for key,value in likes.items():
    print("\nKey: "+key)
    print("Value: "+value)
Key: xiao
Value: python

Key: wang
Value: r

Key: liu
Value: c++
for key in likes.keys():
    print(key)
xiao
wang
liu
for value in likes.values():
    print(value)
python
r
c++
river={"a":"1","b":"2","c":"3"}
for key,value in river.items():
    print("The "+key+" runs through "+value)
The a runs through 1
The b runs through 2
The c runs through 3

第七章 用户输入和while循环

input函数

name=input("Please enter your name:")
print("\nHello, "+ name + "!")
print("I love "+name)
Please enter your name:mary

Hello, mary!
I love mary

int函数

age=input("What's your age:")
age=int(age)
What's your age:21
age_1=input("What's your age:")
What's your age:21
age_1=input("What's your age:")
What's your age:21

求模运算符

4%3  #求余数
1
number=input("How many people:")
number=int(number)
if number>=8:
    print("There' not seat")
else:
    print("There's "+ number +"seats!")
How many people:10
There' not seat
number=input("How many people:")
if int(number)>=8:
    print("There' not seat")
else:
    print("There are "+ number +" seats!")
How many people:6
There are 6 seats!

while语句

#break和continue

第八章 函数

def greet():
    print("hello")
greet()
hello
def greet(user):
    print("hello "+user+"!")
greet("mary")
hello mary!
## 形参和实参
def greet(names):
    for name in names:
        print("hello "+name+"!")

names_=["mary","wang"]
greet(names_)
hello mary!
hello wang!
magic_=["xiao","wang","li"]
def show_magician(names):
    for name in names:
        print(name)

show_magician(magic_)
xiao
wang
li
magic_=["xiao","wang","li"]
magic_x=[]

def make_great(names):
    for name in names:
        print("The Great "+name)
make_great(magic_)
The Great xiao
The Great wang
The Great li
magic_x=["the great "+x for x in magic_]
magic_x
['the great xiao', 'the great wang', 'the great li']
show_magician(magic_)
xiao
wang
li
show_magician(magic_x)
the great xiao
the great wang
the great li
## 禁止函数修改列表,创建副本
def make_great(names):
        for name in names:
            print("The Great "+name)
            
make_great(magic_[:]))
The Great xiao
The Great wang
The Great li
magic_x=
magic_x

show_magician(magic_x)
`


```python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值