Exercise1python
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."
運行結果shell
Notes:less
①非ASCII編碼,在代碼開頭加入以下語句
this
# -*- 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."
輸出spa
③#號表示註釋,其後的內容會被python自動忽略掉命令行
Exercise2debug
代碼code
# 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 / 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
輸出
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."
輸出
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)
輸出
Notes:
①%在這裏是格式化字符串,至關於佔位符同樣的東西
②%r相似%s,不一樣點在於%r主要用於debug
③變量命名中能夠包含字母、數字和下劃線,但不能以數字開頭
④round()用來四捨五入
⑤其餘的格式化字符串
%s 字符串(採用str())顯示
%r 字符串(採用repr())顯示
%c 轉換成字符ASCII碼值或者長度爲1的字符串
%b 二進制整數
%d 十進制整數
%o 八進制整數
%x 十六進制整數
%% 字符%