当前位置: 移动技术网 > IT编程>脚本编程>Python > Python编程从入门到实践一(标题重复率过高?)

Python编程从入门到实践一(标题重复率过高?)

2020年07月30日  | 移动技术网IT编程  | 我要评论
chap2 变量和简单的数据类型转义字符使用双引号lstrip 等strip \n \t都会去掉整数除法// 浮点数除法/int和string型,两个不能相加 str(a)调用等就是a.__str__(),包括None与其他也不能相加chap3-4 列表操作增(append insert),删(pop remove),排序,倒序切片右区间始终是开的前十个整数的平方,并存在列表中打印,简化定义版本;如果需要取元素,老老实实写for循环 for item in list:

chap2 变量和简单的数据类型

  • 转义字符使用
  • python可以+=呀~
  • 双引号 ’ “” ’
  • lstrip 等strip \n \t都会去掉
  • 整数除法// 浮点数除法/
  • int和string型,两个不能相加 str(a)调用等就是a.__str__(),包括None与其他也不能相加

chap3 列表操作

  • 增(append insert),删(pop remove),排序sorted(reverse = True) & .sort(reverse = True),倒序.reverse() 后两个.调用都会改变原来的列表
  • 切片右区间始终是开的
  • 前十个整数的平方,并存在列表中打印,简化定义版本;如果需要取元素,老老实实写for循环 for item in list:
a = [i for i in range(2,11,2)]

names = ['judy','nick','natasha']
divided_name = [i for name in names for i in name ]
print(divided_name)
#ans:
['j', 'u', 'd', 'y', 'n', 'i', 'c', 'k', 'n', 'a', 't', 'a', 's', 'h', 'a']
  • 复制列表2种方式及区别
    复制列表,原列表值不变
    直接a = b 则ab相关联,a变b也变,vice versa
b = a[:]

  PEP 8规范
  1.尽量不要使用小写l(1) o(0)
  2.空行 缩进等的规范

chap4 逻辑条件if

  • in 和 not in
  • if else 怕错点话,在一维里,分析所有的情况,再写程序
  • 帮助人们挣钱的,教育相关,个性化学生学习安排易错点,多维交流(时间空间)平台

chap5 字典

  • 删除del dict_name[key]
  • 遍历 item(),keys(),values()
  • 字典中的东西没有顺序,若想要,则需要sorted (set函数去重)
for fruit_name in sorted(fruit.keys()):
    print(fruit_name)
  • 定义字典必须有键和值
    错误的example:
fruit = {
    'apple':1,
    'banana':2,
}
vegetable={
    'eggplant':1,
    'tomato':2,
}
my_fav_wrong = {fruit,vegetable}
#TypeError: unhashable type: 'dict'

改成列表正确

my_fav_correct = [fruit,vegetable]

如果要字典套字典,只能定义为

my_fav_correct2 = {
    'fruit':{
        'apple':1,
        'banana':2,
    },
    'vegetable':{
        'eggplant':1,
        'tomato':2.
    }
}

chap6 input & while

  • input : 返回的值是string型!
i = input("please enter your age:")
print("Hey,I'm "+ i +" years old")
  • eval() 函数用来执行一个字符串表达式,并返回表达式的值。
eval("3**2")
#ans:
#9
  • while: while循环 quit退出
  • break & continue 和c++一样不写了
prompt = "Tell me sth,and I'll repeat it back to you: "\
         +"\n Enter 'quit' to end the program"
str = ''
while str!='quit':
    str = input(prompt)
    if str !='quit':
        print(str)
练习一 while循环 *退出

注意之前定义了str
再调用str() , 会有not callable的错误!

price = 0
while True:
    age = input("how old are you?")
    if age=='*':
        break
    else:
        age = int(age)
        if age<0:
            print("input again")
            continue
        elif age>=1 and age <=12:
            price = 0
        elif age>12:
            price=12
        print("your cost is "+str(price)+" $.")
练习二 移除元素
orders = ['a','b','c','a','a']
while 'a' in orders:
    orders.remove('a')

chap7 函数

字典作为可变函数参数

  • 函数内对字典进行修改,原来的字典也会进行改变
extra = {'city': 'Beijing', 'job': 'Engineer'}
def person(kw):
    kw['city']='qingdao'
person(extra)
print(extra)
结果:
{'city': 'qingdao', 'job': 'Engineer'}
  • 函数内对字典修改,不会影响到原来的字典。注意调用函数时候字典的写法(类似指针)考虑字典是成对存在**
extra = {'city': 'Beijing', 'job': 'Engineer'}
def person(**kw):
    kw['city']='qingdao'
person(**extra)
print(extra)
结果:
{'city': 'Beijing', 'job': 'Engineer'}

列表元组任意数量实参

def make_pizza(*toppings):  #这里的参数是元组形式
    total = ''
    for index,i in enumerate(toppings):
        print(i)
        total += i
        if index == len(toppings)-1:
            total+='.'
        else:
            total+=','
    print("make a pizza: "+total)

toppings = ['mushrooms','green peppers','extra cheese'] # 或者定义成元组
make_pizza(*toppings)

模块找不到错误
将函数存在 a.py 文件下,在同一个路径里 b.py 调用
直接from b import * ,然后调用 函数() 报错找不到
‘’’
当我们导入一个模块时:import xxx,默认情况下python解析器会搜索当前目录、已安装的内置模块和第三方模块,搜索路径存放在sys模块的path中
‘’’


import sys
sys.path.append("根目录")

本文地址:https://blog.csdn.net/weixin_42277616/article/details/107633177

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网