《编程小白的第一本python入门书》——读书笔记
a = 15
print( str(a) + '1')
//#结果为151
//#(猜测):str() 可以将int型数据转化成字符串,小数也可以这样做
def fahrenheit_converter(C):
fahrenheit = C * 9/5 + 32
return str(fahrenheit) + '。F'
C2F = fahrenheit_converter(35)
//#就是将fahrenheit_converter()函数的返回值赋值给C2F
//#调用函数好比是call,return是对call的回应,若函数中没有return,则在调用(call)的时候不会得到回应(none)
print(C2F)
(调用函数)
import 模块
模块.函数
print的sep参数可以在每个结尾设置格式
给函数设置默认参数
//#创建文件 涉及文件的写
def text_create(name,msg):
desktop_path = 'C:/Users/xyh/Desktop/new/OJ/'
full_path = desktop_path + name + '.txt'
file = open(full_path,'w')
file.write(msg)
file.close()
print("done")
text_create('hello','hello world')
def text_filter(word, censored_word = 'lame', changed_word = 'awesome'):
print(word.replace(censored_word,changed_word))
text_filter('python is lame')
//#是world.replace(旧的,新的)
True and False
python控制台(console)可以像命令行一样使用
!= 和 <>一样
当两个变量一致时,经过is对比后就会返回True
range(1,11) 实际范围是1~10
列表、字典、元组、集合
这样做可将orange加入到fruit列表的最前面
列表只接受用位置进行索引,若要用元素进行索引,则需要用到字典
key和value是一一对应的,key是不可变的
集合可以被添加和删除,但不能被切片和索引(字典不能被切片)
num_list = [6,2,7,4,1,3,5]
print(sorted(num_list))
//#sorted函数按照长短、大小、英文字母的顺序给每个列表中的元素
//#进行排序,在使用默认参数reverse后,列表可以被按照逆序整理
print(sorted(num_list,reverse = True))
在整理列表的过程中,如皋要用到两个列表,就使用zip函数
for a,b in zip(num,str):
print(b,'is',a)
推导式即列表的解析式(list comprehension)
import string
path = 'C:/Users/xyh/Desktop/new/OJ/Walden.txt'
with open(path,'r',encoding='utf-8') as text:
words = [raw_words.strip(string.punctuation).lower() for raw_words in text.read().split()]
words_index = set(words)
counts_dict = {index:words.count(index) for index in words_index}
for word in sorted(counts_dict,key=lambda x: counts_dict[x],reverse=True):
print('{}={} times'.format(word,counts_dict[word]))
类
class Cocacola:
formula = ['caffrine', 'sugar', 'water', 'soda']
coke_for_me = Cocacola()
coke_for_you = Cocacola()
print(Cocacola.formula)
print(coke_for_me.formula)
print(coke_for_you.formula)
class Cocacola:
formula = ['caffrine', 'sugar', 'water', 'soda']
def drink(self):
print('energy!')
coke = Cocacola()
coke.drink()
self其实就是被创建的实例本身
class Cocacola():
formula = ['caffeine', 'sugar', 'water', 'soda']
def __init__(self):
// # self.local_logal = '可口可乐'
for element in self.formula:
print('Coke has {}'.format(element))
def drink(self):
print('Energy')
coke = Cocacola()
//# print(coke.local_logal)
class Cocacola():
formula = ['caffeine', 'sugar', 'water', 'soda']
def __init__(self,local_name):
self.local_logo = local_name
def drink(self):
print('Energy')
coke = Cocacola('可口可乐')
print(coke.local_logo)
class Cococola():
calories = 140
sodium = 45
total_carb = 39
caffeine = 34
ingredients = [
'High Fructose Corn Syrup',
'Carbonated Water',
'Phosphric Acid',
'Natural Flavors',
'Caramel Color',
'Caffeine'
]
def __init__(self,logo_name):
self.local_logo = logo_name
def drink(self):
print('You got {} cal energy!'.format(self.calories))
# 类的继承
class CaffeineFree(Cococola):
caffeine = 0
ingredients = [
'High Fructose Corn Syrup',
'Carbonated Water',
'Phosphric Acid',
'Natural Flavors',
'Caramel Color'
]
coke_a = CaffeineFree('Cocacocl-FREE')
coke_a.drink()
_dict_是一个类的特殊属性,它是一个字典,用于存储类或者实例的属性。即使不去定义它,它也会存在于每一个类中,是默认隐藏的。
…看完了~
以上。
2021年1月31日