python學習記錄(1)

#2016-01-12
#(1)python3.5.1在Linux環境的安裝;python的編輯編譯與運行;

#(2)輸出print;輸入input;

#(3)變量與賦值、bool變量、數組


一、第一個python程序
[root@localhost pywork]# cat hello.py 
print ('hello,world!')
print ('This is my first python code')
print (309+418)
運行結果:
[root@localhost pywork]# python hello.py 
hello,world!
This is my first python code
727


二、print()輸出
[root@localhost pywork]# cat print.py 
print ('hello,world')

print ('The quick brown fox','jumps over')
#print()中的“ ,“ 將兩個多個輸出隔開,自身顯示爲空格


print ('309+418 =',309+418)


print('1024*768 = ',1024*768)


print('I\'m ok.')
# 反斜槓\ :轉義


print ('I\'m learning\nPython.')


print ('\\\n\\')
# \本身也需要轉義


print('\\\t\\')
print(r'\\\t\\')
# r'  ' 用來表示:內部字符不轉換


#換行的輸出方法?

運行結果:
[root@localhost pywork]# python print.py 
hello,world
The quick brown fox jumps over
309+418 = 727
1024*768 =  786432
I'm ok.
I'm learning
Python.
\
\
\ \
\\\t\\


三、input( )輸入

[root@localhost pywork]# cat input.py 

name = input('hello,what\'s your name?\n')
print('hello',name)

運行結果:
[root@localhost pywork]# python input.py 
hello,what's your name?
Jelly
hello Jelly


四、bool型變量
[root@localhost pywork]# cat bool.py 
#bool值只有兩種:True和False
b1=True
b2=False
b3=3>2
b4=3>5
print(b1,b2,b3,b4)


#bool值可以進行 and、or、not運算
a1=True and False
a2=True or  False
a3=not True
print(a1,a2,a3)


#none 是一個特殊的空值

運行結果:
[root@localhost pywork]# python bool.py 
True False True False
False True False


五、python中的變量

[root@localhost pywork]# cat variable.py 

#變量的賦值
a=123
print(a)
a='abc'
print(a)


x=10
x=x+2
print(x)


#理解一個變量賦值的過程
# 例如:a='abc'
#       (1)內存中創建了abc字符串
#       (2)內存中創建了a,並將它指向'abc'
a='abc'
b=a
a='xyz'
print(b)


#變量的運算
x1=10/3
x2=10.0/3
x3=10%3
print(x1)
print(x2)
print(x3)

運行結果:
[root@localhost pywork]# python variable.py 
123
abc
12
abc
3.3333333333333335
3.3333333333333335

1

                            



六、python中的序列(數組)

[root@localhost pywork]# cat sequence.py 

#sequence
gg1=['Taeyeon Kim',26]
gg2=['Jessica Jung',26]
datebase=[gg1,gg2]
print(datebase)


#數組中下標從0開始,-1表示最後一個元素
greeting='hello'
print(greeting[0])
print(greeting[-1])


#不賦值而直接使用
fourth=input(r'Year: ')[3]
print(fourth)


#test2-1 
#輸出給定格式的日期
#示例:
#   Year:1974
#   Month(1-12):8
#   Day(1-31):16
#輸出結果:August 16th,1974


   #說明:英語日期中的ending:
   #1、21、31(1結尾的)後綴爲21st
   #2、22(2結尾的)後綴爲22nd
   #3、23(3結尾的)後綴爲23rd
   #11、12、13和其他均爲th


months=['January','February','March','April','May','June','July','August','September','October','November','December']

endings=['st','nd','rd']+17*['th']+['st','nd','rd']+7*['th']+['st']

year=input(r'Year: ')
month=input(r'Month(1-12): ')
day=input(r'Day(1-31): ')

month_name=months[int(month)-1]      
day_name=day+endings[int(day)-1]     
print(month_name+' '+day_name+','+year)


運行結果:
[root@localhost pywork]# python sequence.py 
[['Taeyeon Kim', 26], ['Jessica Jung', 26]]
h
o
Year: 1995
5
Year: 2016
Month(1-12): 3
Day(1-31): 2

March 2nd,2016

#之前沒有加int時month_name=months[month-1]、day_name=day+endings[day-1],有報錯:

               


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章