笨方法学 python 笔记_笨方法学Python,Lesson1,2,3,4,5

Exercise1

print "Hello World!"

print "Hello Again"

print "I like typing this."

print "This is fun."

print 'Yay! Printing.'

print "I'd much rather you 'not'."

print 'I "said" do not touch this.'

print "This is another line."

运行结果

150752_kfC3_2297516.png

Notes:

①非ASCII编码,在代码开头加入如下语句

# -*- coding:utf-8 -*-

②语句末尾加逗号,可以使其只打印一行

print "Hello World!",

print "Hello Again",

print "I like typing this.",

print "This is fun.",

print 'Yay! Printing.',

print "I'd much rather you 'not'.",

print 'I "said" do not touch this.',

print "This is another line."

输出

151626_gL88_2297516.png

③#号表示注释,其后的内容会被python自动忽略掉

Exercise2

代码

# A comment, this is so you can read your program later.

# Anything after the # is ignored by python.

print "I could have code like this." # and the comment after is ignored.

#You can also use a comment to "disable" or comment out a piece of code:

# print "This's won't run."

print "This will run."

输出

153111_SvgB_2297516.png

Notes:

①#后内容表示注释,也可以用于忽略不想执行的部分代码

②引号中的#不表示注释,而是作为字符串的一部分

>>> print "#######"

#######

Exercise3

代码

print "I will now count my chickens:"

print "Hens", 25.0 + 30 / 6

print "Roosters", 100.0 - 25 * 3 % 4

print "Now I will count the eggs:"

print 3.0 + 2 + 1 - 5 + 4 % 2 - 1.0 / 4 + 6

print "Is it true that 3 + 2 < 5 - 7?"

print 3 + 2 < 5 - 7.0

print "What is 3 + 2?", 3.0 + 2

print "What is 5 - 7?", 5.0 - 7

print "Oh, that's why it's False."

print "How about some more."

print "Is it greater?", 5.0 > -2

print "Is it greater or equal?", 5.0 >= -2

print "Is it less or equal?", 5.0 <= -2

输出

153731_HjEN_2297516.png

Notes:

①命令行中启动python交互环境,可以把python当作计算器~

②注意"/"和"//"的区别,前者进行的是真正的除法,分子、分母均为整型则结果也为整型,小说部分直接舍去;分子、分母中有浮点型则结果也为浮点型。后者进行的是地板除,除的结果都是整数。

③百分比号%在数学计算中用于取余

④计算数序为PEMDAS,即括号、指数、乘除再加减。乘除、加减优先级相同。

Exercise4

代码

cars = 100

space_in_a_car = 4.0

drivers = 30

passengers = 90

cars_not_driven = cars - drivers

cars_driven = drivers

carpool_capacity = cars_driven * space_in_a_car

average_passengers_per_car = passengers / cars_driven

print "There are", cars, "cars available."

print "There are only", drivers, "drivers available."

print "There will be", cars_not_driven, "empty cars today."

print "We can transport", carpool_capacity, "people today."

print "We have", passengers, "to carpool today."

print "We need to put about", average_passengers_per_car, "in each car."

输出

154642_y1zy_2297516.png

Notes:

①"_"下划线在变量命名中用作假想的空格

②"="用来赋值,"=="用来判断等号两边的对象的值是否相等

③较好的代码风格是"="赋值时前后加上空格,其他操作符同理

a = 123

123 + 456

123 - 456

123 * 456

123 // 456

Exercise5

代码

my_name = "Jer Chou"

my_age = 23

my_height = 175

my_weight = 60

my_eyes = "Black"

my_teeth = "White"

my_hair = "Black"

print "Let's talk about %s." % my_name

print "He's %d cm tall." % my_height

print "He's %d KG heavy." % my_weight

print "Actually that's not too heavy."

print "He's got %s eyes and %s hair." % (my_eyes,my_hair)

print "His teeth are usually %s depending on the coffee." % my_teeth

# this line is tricky,try to get it exactly right

print "If I add %d, %d, and %d I get %d." % (

my_age,my_height,my_weight,my_age + my_height + my_weight)

输出

172029_0O46_2297516.png

Notes:

①%在这里是格式化字符串,相当于占位符一样的东西

②%r类似%s,不同点在于%r主要用于debug

③变量命名中可以包含字母、数字和下划线,但不能以数字开头

④round()用来四舍五入

⑤其他的格式化字符串

%s 字符串(采用str())显示

%r 字符串(采用repr())显示

%c 转换成字符ASCII码值或者长度为1的字符串

%b 二进制整数

%d 十进制整数

%o 八进制整数

%x 十六进制整数

%% 字符%

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值