python3 _笨方法學Python_日記_DAY1


第四版 資源https://pan.baidu.com/s/1bqvf0Ov

從零開始!每個細節都要過

書中是根據python2.5寫的

我用的是py3,順便熟悉一下兩者的區別

 

  • Day1 
  • “習題一:第一個程序”

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.')

結果:

hello world!
Hello again
I like typing this.
This is fun.
Yay!Printing.
I'd much rather you 'not'.

I "said" do not touch this.

嘗試:玩一玩單引號和雙引號

 

#單引號雙引號嘗試
print('1')
print("1")

1

1

似乎是一樣的,那么

print('1")

  File "E:/py/learn python the hard way/Day1/01.py", line 12
    print('1")
             ^

SyntaxError: EOL while scanning string literal

字符串,引號沒有成對出現,報錯,看來並不完全一樣

再試試

 

print("I'd love to")
print('I'd love to')

第二個報錯

    print('I'd love to')
             ^

SyntaxError: invalid syntax

所以雙引號可以避免這種句子中需要單引號的情況

 

  • 習題  3:  數字和數學計算

print("I'll now count my chickens:")
print("Hens",25+30/6)
print("Roosters",100-25*3%4)
print("Now I will count the eggs:")
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
print("It is true that 3+2<5-7?")
print(3+2<5-7)
print("what is 3+2?",3+2)
print("what is 5-7?",5-7)
print("Oh that's why it is false.")
print("How about some more.")
print("Is it greater?",5-2)
print("Is it greater or equal?",5>=-2)
print("Is it less or equal?",5<=-2)

Hens 30.0
Roosters 97
Now I will count the eggs:
6.75
It is true that 3+2<5-7?
False
what is 3+2? 5
what is 5-7? -2
Oh that's why it is false.
How about some more.
Is it greater? 3
Is it greater or equal? True
Is it less or equal? False

結果與書上有出入的有兩行,應該是PY2和PY3的區別

print("Hens",25+30/6)
30.0
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
6.75

書上結果為30和7

主要在於除法的運算上,

py2:一個整數(無小數部分的數)被另外一個整數除,計算結果的小數部分被截除了,只留下了整數部分

所以1/4=0 所以第二條結果為7

在Python3.0中變成真除法(無論任何類型都會保持小數部分,即使整除也會表示為浮點數形式)

  • 習題4:變量(variable)和命名

 

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")

結果

there are 100 cars available
there are only 30 drivers available
there will be 70 empty cars today
we can transport 120.0 people today
we have 90 to carpool today
we need to put about 3.0 in each car

加分題:

Traceback (most recent call last):
File "ex4.py", line 8, in <module>
average_passengers_per_car = car_pool_capacity /
passenger
NameError: name 'car_pool_capacity' is not defined

#報錯原因:car_pool_capacity 沒有被定義

  • 習題5:更多的變量和打印

 

 

my_name='mrfri'
my_age=23
my_height=178#cm
my_weight=60#kg
my_eyes='Brown'
my_teeth='White'
my_hair='Black'
print("Let's talk about %s."%my_name)
print("He is %d cms high."%my_height)
print("He is %d kgs heavy."%my_weight)
print("He's got %s eyes and %s hair.His teeth are %s."%(my_eyes,my_hair,my_teeth))
print("If I add %d,%d and %d,then I get: %d."%(my_age,my_weight,my_height,my_age+my_height+my_weight))

Let's talk about mrfri.
He is 178 cms high.
He is 60 kgs heavy.
He's got Brown eyes and Black hair.His teeth are White.
If I add 23,60 and 178,then I get: 261.

格式化字符:

%% 百分號標記 #就是輸出一個%
%c 字符及其ASCII碼
%s 字符串
%d 有符號整數(十進制)
%u 無符號整數(十進制)
%o 無符號整數(八進制)
%x 無符號整數(十六進制)
%X 無符號整數(十六進制大寫字符)
%e 浮點數字(科學計數法)
%E 浮點數字(科學計數法,用E代替e)
%f 浮點數字(用小數點符號)
%g 浮點數字(根據值的大小采用%e或%f)
%G 浮點數字(類似於%g)
%p 指針(用十六進制打印值的內存地址)
%n 存儲輸出字符的數量放進參數列表的下一個變量中

%r 照原樣打印出來,例如字符串會打印出引號

測試:

first='a'
second=1-17
third=0.0008320320156

print("first is %c"%first)
print("有符號整數%d十進制"%second)
print("無符號整數%u十進制"%second)
print("無符號整數%o八進制"%second)
print("無符號整數%x十六進制"%second)
print("浮點數字%f小數點"%third)
print("浮點數字%e科學計數法"%third)
print("自動判斷用e還是f,%g"%third)

有符號整數-16十進制
無符號整數-16十進制
無符號整數-20八進制
無符號整數-10十六進制
浮點數字0.000832小數點
浮點數字8.320320e-04科學計數法
自動判斷用e還是f,0.000832032

可見,似乎在python3中不存在有無符號的說法,統一打成有符號的?

 

今天就到這吧,從實例入手感覺還不錯,比較好玩,比光看書有意思多了。

堅持!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM