《编程小白的第一本python入门书》——读书笔记

《编程小白的第一本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日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值