Python编程:从入门到实践【2】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/itsxwz/article/details/82191039

1.if语句

  1. 在Python中,两个大小写不同的值会视为不相等。
  2. != 号 , 检查是否不相等
  3. 条件1 and 条件2 (与)
  4. 条件1 or 条件2 (或)
  5. num in nums #检查特定值是否包含在列表中
  6. if num not in nums: #检查特定值是否不包含在列表中
#if语句
if age > 24:
    print('older than me')

#if-else语句
if age > 24:
    print('older than me')
else
    print('younger than me')

#if-elif-else语句
if age > 24:
    print('older than me')
elif age > 60:
    print('too old')
else
    print('youger than me')

2.字典

alien = {'color':'green','points':'5'}  #字典  (花括号表示  键值对)
alien['color']  #返回该键对应的值
alien['sex'] = 'man' #添加键值对
alien['sex'] = 'women'  #修改字典中的值
del alien['sex']  #删除键值对

for k,v in alien.items():  #遍历字典的键值对  (字典名.items())
for k in alien.keys():  #遍历字典的键  (字典名.keys())
for v in alien.values():  #遍历字典的值  (字典名.values())
#字典列表  每一个列表元素都是一个字典
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_2]  

#字典中存储列表
pizza = {
    'crust': 'thick',
    'toppings': ['mushrooms', 'extra cheese'],
    }

#字典中存储字典
users = {
    'aeinstein': {
        'first': 'albert',
        'last': 'einstein',
        'location': 'princeton',
        },
    'mcurie': {
        'first': 'marie',
        'last': 'curie',
        'location': 'paris',
        },
}

3.输入和while循环

input('Please input a string')  #引号内的字符串会先显示给用户,这个函数会返回用户输入的字符串

int(28)
float(28)  #会把字符串28转换为整型、浮点型数据  int.Prase() float.Prase()

while 条件:
    #do something

break  #退出整个循环
continue  #跳过本次循环

#删除包含特定值的所有列表元素  (remove()  删除第一个)
while 'cat' in pets:
    pets.remove('cat')

4.函数

#函数定义
def greet_user():
    print("Hello!")  #函数体

#函数调用
greet_user()
#向函数传递信息
def greet_user(username):
    print("Hello, " + username.title() + "!")

greet_user('jesse')
#关键字实参
def describe_pet(animal_type, pet_name):
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")

describe_pet(animal_type='hamster', pet_name='harry')
#默认值
def describe_pet(pet_name, animal_type='dog'):
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")

describe_pet(pet_name='willie')
#返回值  (没有返回类型 略略略)
def get_formatted_name(first_name, last_name):
    full_name = first_name + ' ' + last_name
    return full_name.title()
#传递列表的副本给函数,通过切片的方式
function_name(list_name[:])
#传递任意数量的实参
def make_pizza(*toppings):
    print(toppings)
#使用任意数量的关键字实参
def build_profile(first, last, **user_info):
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last 

    for key, value in user_info.items():
        profile[key] = value

    return profile 

user_profile = build_profile( 
    'albert', 'einstein',
    location='princeton',
    field='physics') 

print(user_profile)
import module_name  #导入整个模块文件(module_name.py)
from module_name import function_name  #导入特定的函数
from module_name import function_name as fn  #使用as给函数指定别名
import module_name as mn  #使用as给模块指定别名
from module_name import *    #使用*号导入模块中的所有函数

猜你喜欢

转载自blog.csdn.net/itsxwz/article/details/82191039