python入门基础语法答案_Python入门基础语法汇集

第一部分:

1.python标准约定文件名:使用小写字母和下划线,如:simple_message.py和simple_messages.py.

2.修改字符串的大小写:

message = "xiejunbo,heLLO woRLD"

print(message.title())

print(message.upper())

print(message.lower())

3.合并字符串直接+号连接 .

4.换行符号\n   特殊符号可加转义字符\

5.删除空白:

name = "xiejunbo "

print(name.rstrip())

6.在pyhon2中无需将打印内容放在括号内。而python3中的print是一个函数,因此括号必不可少。python2代码有些包含括号,有些不包含。

7.

first_name = "xie"

last_name = "junBO"

print(first_name + " " + last_name)

print(first_name.upper() + " " + last_name.upper())

print(first_name.title() + " " + last_name.title())

8.去空格

first_name = " xie "

print("result:" + first_name)

print("result:" + first_name.lstrip())

print("result:" + first_name.rstrip())

print("result:" + first_name.strip())

9.转为字符串类型

age = 27

message = "Happy " + str(age) + "rd Birthday!"

print(message);

10.注释: 用#号注释

11.使用sort()对数组进行永久排序

12.使用sorted()对数组进行临时排序

13.逆序打印数组reverse()

14.列表长度len(list)

15.

#按顺序生成范围内数字

numbers = range(0,10)

for value in numbers:

print(value)

#list()将range()的结果直接转为列表

_list = list(numbers)

print(_list)

print(max(numbers))#最大值

print(min(numbers))#最小值

print(sum(numbers))#求和

16.列表解析的写法(实际是for循环缩成一行)

list = [value**2 for value in range(1,9)]

print(list)

#输出结果:[1, 4, 9, 16, 25, 36, 49, 64]

17.复制列表

users = ['fett','batt','gatt']

vips = users[:]

print(users)

print(vips)

#输出:['fett', 'batt', 'gatt']

['fett', 'batt', 'gatt']

18.不可变的列表称为元组,需要存储的一组值在整个程序的生命周期内都不变时可使用元组

dimensions = (100,200)

print(dimensions)

print(dimensions[0])

print(dimensions[1])

#输出:(100, 200)

# 100

# 200

19.代码规范:

每级缩进使用4个空格,尽量控制代码行长80以内,注释行长72以内.

20.将一变量的当前值与特定值进行比较

name = "xiejunbo"

print(name == "xiejunbo")

#output:True

#是否不相等

print(name != "xiejunbo" and name != "zm")

21.检查特定值是否包含在列表

#检查特定值是否不包含在列表中

users = ['fett','batt','gatt','zm']

user = "xiejunbo"

user2 = "zm"

if user not in users:

print("user not in users")

elif user2 in users:

print("user2 not in users")

else:

print("in users")

22.

#布尔值

vip = True

oVip = False

if (vip):

print(vip)

else:

print(oVip)

#判断列表是否为空

tops = []

if tops:print(tops)

else:print("list is null")

23.字典操作

#字典,实际上跟java的map一样,存储一系列键值对

#创建字典

alien = {}

alien["first_name"] = "xie"

alien["last_name"] = "junbo"

alien["job"] = "developer"

print(alien)

#output: {'last_name': 'junbo', 'job': 'developer', 'first_name': 'xie'}

#修改字典值

alien["job"] = "programmer"

print(alien)

#output:{'last_name': 'junbo', 'job': 'programmer', 'first_name': 'xie'}

#删除字典

del alien["job"]

print(alien)

#output:{'last_name': 'junbo', 'first_name': 'xie'}

#遍历字典键值对

for key, value in alien.items():

print("key:" + key)

print("value:" + value)

#output:key:last_name

# value:junbo

# key:first_name

# value:xie

#遍历字典中所有键, 所有值用values()

for key in alien.keys():

print(key.title())

#output: Last_Name

# First_Name

#按顺序遍历字典中所有键,实际先排序再遍历

for key in sorted(alien.keys()):

print(key.title())

24.用户输入

#input()等待用户输入并输出内容

message = input("Please input someth and I will return it to you:")

print(message)

#int() 获取数值输入

age = input("How old are you?")

age = int(age)

print(age)

#Please input someth and I will return it to you:18

#18

#How old are you?

25.用户输入填充字典

responses = {}

#设置一个标识位,指出调查是否继续

polling_active = True

while polling_active :

#提示输入被调查者的名字和回答内容

name = input("What's your name?")

response = input("Which mountain would you like to climb someday?")

#将答卷存在字典中

responses[name] = response

#看是否还有人要参与调查

repeat = input("Would like to let another person respond? (yes/no)")

if repeat == 'no':

polling_active = False

#调查结束,显示结果

print("\n---poll result---")

for name, response in responses.items():

print(name + "would like to climb " + response + ".")

#What's your name?xiejunbo

#Which mountain would you like to climb someday?Baiyun mountain

#Would like to let another person respond? (yes/no)yes

#What's your name?zm

#Which mountain would you like to climb someday?Fengyun mountain

#Would like to let another person respond? (yes/no)no

#---poll result---

#xiejunbowould like to climb Baiyun mountain.

#zmwould like to climb Fengyun mountain.

26.函数

#指定形参默认值的函数

def greet_user(name = "zm"):

"""Warnly welcome"""

print("函数内部打印:热烈欢迎" + name)

return name

print("外部打印:" + greet_user("xiejunbo"))

print("内部打印:" + greet_user())

#output:

#函数内部打印:热烈欢迎xiejunbo

#外部打印:xiejunbo

#函数内部打印:热烈欢迎zm

#内部打印:zm

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值