《趣学Python编程》笔记---第一部分:学习编程(1)


本书课后练习题代码:

http://download.csdn.net/detail/gongzaiwenzi/8009071

本书英文PDF版本:

http://www.open-open.com/doc/view/1033ad2f0a0845e8aab0e9abab2b5a7f


第三章 字符串、列表、元祖和字典
1、用“”或者‘’来输入字符串,并用print输出
2、列表中加上和删去元素

>>> wizard_list=['a','b']
>>> print(wizard_list)
['a', 'b']
>>> wizard_list.append('bear')
>>> print(wizard_list)
['a', 'b', 'bear']
>>> del wizard_list[2]
>>> print(wizard_list)
['a', 'b']

3、元祖是用括号()表示的列表,与列表的主要区别在于创建之后不能做改动。
4、字典里每一个元素都有KEY—对应的VALUE
5、逗号输出自动变成空格

num_house=3
num_wuding=25
num_didao=2
num_didaos=40
print("忍者数量等于:",num_house*num_wuding)
print("武士数量等于:",num_didao*num_didaos*num_house)
输出结果:
忍者数量等于: 75
武士数量等于: 240

第四章 用海龟画图
1.注意:Pen 中P一定要大写

>>> import turtle
>>> t=turtle.Pen()
一些指令
>>> t.forward(50)
>>> t.forward(50)
>>> t.back(50)
>>> t.left(90)
>>> t.forward(50)
>>> t.left (90)
>>> t.forward(50)
>>> t.reset()**//清空画布**
>>> t.back(50)
>>> t.up()//抬起画笔
>>> t.left (90)
>>> t.forward(50)
>>> t.right (90)
>>> t.down()//放下画笔
>>> t.forward (50)

第五章 用if和else来提问
1.python中的空白是有意义的,处于同一位置的代码代表同一个代码块。
只要新起了一行(前面的留白不同),就代表一个新的代码块。[外链图片转存中…(img-swhxU3K3-1569659281514)]
2.if-else 代码示例

>>> age=10
>>> if age==10:
	print("1")
else:
	print("2")	

3.elif==else if

>>> age=10
>>> if age==12:
	print("1")
elif age==10:
	print("2")
else:
	print("3")

4.字符串与数字之间的相互转换
int/float(字符串)变成数字
str(数字)变成字符串


第六章 循环

>>> for i in range(0,5):
	print("hello")

	
hello
hello
hello
hello
hello

2.第二种用法

>>> wizard_list=['you','me','he','she']
>>> for i in wizard_list:
	print(i)

	
you
me
he
she
>>> 

表示对于 wizard_list里的每个元素,放入变量i,然后打印,直到执行完列表

3.Python语言中的嵌套循环结构,代码如下

for i in wizard_list:
print(i)
for j in wizard_list:
print(j)
you
you
me
he
she
me
you
me
he
she
he
you
me
he
she
she
you
me
he
she

第七章 使用函数和模块来重用你的代码
1.定义函数–调用函数–参数赋值–计算得出结果

>>> def hanshu1(canshu1):
	print("hello %s"%canshu1)

	
>>> hanshu1('mary')
hello mary
>>> def saving(zhengde,jiande,huade):
	return zhengde+jiande-huade

>>> qian=saving(10,0,5)
>>> print(qian)
5

7.1.2 变量与作用域
在函数内定义的变量只可以在函数内用
即全局变量与部分变量的区别
飞船罐头(示例)

def spaceship_building(cans):
    sum=0
    for week in range(1,53):
        sum=sum+cans
        print("week %s=%s"%(week,sum))
spaceship_building(2)

7.2 使用模块
模块类似于C语言中头文件,必须先用import(引入)
常用:import time //sys//turtle
7#月球体重程序

import sys
def hanshu():
    print("请输入您的体重")
    qishi=(int)(sys.stdin.readline())
    print("请输入每年增长的体重")
    zj=(float)(sys.stdin.readline())
    print("请输入年份")
    year=(int)(sys.stdin.readline())
    for i in range(1,year+1):
        qishi=qishi+zj
        print("第%s年 体重=%s"%(i,qishi*0.165))
hanshu()

第八章 如何使用类和对象
1.类别如何定义,子类与父类的定义关系

class Things:
    pass
class animals(Things):
    def breathe(self):
        print("breathing")
    def move(self):
        print("moving")
    def eat(self):
        print("eating")
class mammals(animals):
    def feed_kids(self):
        print("feeding")
class giraffes(mammals):
    def eat_leaves(self):
        print("eat_leavesing")
ok=giraffes()
ok.move()
ok.eat_leaves()
fine=giraffes()
fine.move()

2.子类可以随便调用父类中定义的所有函数------函数继承

8.2.2 从函数里调用其它函数

class Things:
    pass
class animals(Things):
    def breathe(self):
        print("breathing")
    def move(self):
        print("moving")
    def eat(self):
        print("eating")
class mammals(animals):
    def feed_kids(self):
        print("feeding")
class giraffes(mammals):
    def eat_leaves(self):
        self.eat()
    def find_food(self):
        self.move()
        print("i've found food!")
        self.eat()
    def dance(self):
        self.move()
ok=giraffes()
fine=giraffes()
ok.dance()

8.3初始化对象
(注意是两个下划线一共四个!!)__init__函数----Python类里面的一中特殊类型的函数,并且只能叫这个名字,这个函数是在对象被创建的同时就设置它的属性的一种方法。

class giraffes:
	def __init__(self,spots):
		self.giraffe_spot=spots
ok=giraffes(100)
print(ok.giraffe_spot )
100

注意:1.参数赋值是赋值给spots

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值