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."
运行结果
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."
输出
③#号表示注释,其后的内容会被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."
输出
Notes:
①#后内容表示注释,也可以用于忽略不想执行的部分代码
②引号中的#不表示注释,而是作为字符串的一部分
>>> print "#######"#######
Exercise3
代码
print "I will now count my chickens:"print "Hens", 25.0 + 30 / 6print "Roosters", 100.0 - 25 * 3 % 4print "Now I will count the eggs:"print 3.0 + 2 + 1 - 5 + 4 % 2 - 1.0 / 4 + 6print "Is it true that 3 + 2 < 5 - 7?"print 3 + 2 < 5 - 7.0print "What is 3 + 2?", 3.0 + 2print "What is 5 - 7?", 5.0 - 7print "Oh, that's why it's False."print "How about some more."print "Is it greater?", 5.0 > -2print "Is it greater or equal?", 5.0 >= -2 print "Is it less or equal?", 5.0 <= -2
输出
Notes:
①命令行中启动python交互环境,可以把python当作计算器~
②注意"/"和"//"的区别,前者进行的是真正的除法,分子、分母均为整型则结果也为整型,小说部分直接舍去;分子、分母中有浮点型则结果也为浮点型。后者进行的是地板除,除的结果都是整数。
③百分比号%在数学计算中用于取余
④计算数序为PEMDAS,即括号、指数、乘除再加减。乘除、加减优先级相同。
Exercise4
代码
cars = 100space_in_a_car = 4.0drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * space_in_a_caraverage_passengers_per_car = passengers / cars_drivenprint "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."
输出
Notes:
①"_"下划线在变量命名中用作假想的空格
②"="用来赋值,"=="用来判断等号两边的对象的值是否相等
③较好的代码风格是"="赋值时前后加上空格,其他操作符同理
a = 123123 + 456123 - 456123 * 456123 // 456
Exercise5
代码
my_name = "Jer Chou"my_age = 23my_height = 175my_weight = 60my_eyes = "Black"my_teeth = "White"my_hair = "Black"print "Let's talk about %s." % my_nameprint "He's %d cm tall." % my_heightprint "He's %d KG heavy." % my_weightprint "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 rightprint "If I add %d, %d, and %d I get %d." % ( my_age,my_height,my_weight,my_age + my_height + my_weight)
输出
Notes:
①%在这里是格式化字符串,相当于占位符一样的东西
②%r类似%s,不同点在于%r主要用于debug
③变量命名中可以包含字母、数字和下划线,但不能以数字开头
④round()用来四舍五入
⑤其他的格式化字符串
%s 字符串(采用str())显示
%r 字符串(采用repr())显示
%c 转换成字符ASCII码值或者长度为1的字符串
%b 二进制整数
%d 十进制整数
%o 八进制整数
%x 十六进制整数
%% 字符%