Skip to main content

[Python] Notebook-2 Introduction to Python: Absolute Beginner


在準備申請研究所相關文件的同時,
我也開始上一些Microsoft開的Python課程,
很基礎的那種,
畢竟我上一次寫程式已經是兩、三年前的事了。
後來就都是寫HSpice這種描述與模擬電路的語言。
我是從Introduction to Python: Absolute Beginner開始著手,
對。
絕對菜鳥。
我就是這麼菜。
但是我還是(很厚顏的)把自己整理的筆記提供給大家。

內容有string的構成、print() function、string concatenation、type function、.isalpha function alike、Boolean in keyword、Function Customization、if elif else test和基礎數學運算。

補充說明一下,
要在Blogger插入程式碼請參照[技術分享] 寫給會在部落格中撰寫程式碼的你 ─ 在網頁中嵌入高亮程式碼上色 (syntax highlighting)
而且關於CSS的部分要看該篇文章下方留言,
按照該篇文章作者的方式會出現錯誤喔!
我也把我的CSS和JS檔案上傳到資料夾,
我的檔案支援套色的語言有Markup, CSS, C-like, JavaScript, C, C++, Matlab, Python, R, Verilog, VHDL, vim。
外掛套件則有line-numbers+show-language。



# -*- coding: utf-8 -*-
"""
Created on Fri Aug  4 14:46:10 2017
@author: ShihHsing Chen
This is a note for the class 
"Introduction to Python: Absolute Beginner".
"""

#"Kevin Chen" is a string.
student_name = "Kevin Chen"
print(student_name)

#We can use + to add strings, floats and integers.(only the same data type)
#And we also can use commas to add them together.(can be different types)
#25 is an integer but "25" is a string.
beverb = "is"
print(student_name + " " + beverb + " 25.")
print(student_name, beverb, 25.)
print(student_name, beverb, "25,", "and an engineer.")
print()

#We can reassign a new string to the variable student_name
#And type() can check data types.
student_name = 'Richy Chaung'
print(student_name)
print(type(student_name))
print()

#Anything passing through input() becomes a string. 
#But we can cast the string into other data types.
#These functions include int() float() str() 
print("enter your weight:")
your_weight= input()
print("Your weight is:", your_weight)
your_weight2 = input("enter your weight:")
print("Your weight is:", your_weight2)

#There are several function starting with "is" to check the 
#character is capital or not. 
#Like .isalpha() .isalnum() .startswith("a character you wanna test")
# .isupper() .islower() .istitle() .isdigit()
#Besides, .upper() .lower() .capitalize() .title() .swapcase()
#can be used for string formatting.
time_slot = "5secs"
print(time_slot.isalnum())
print(time_slot.upper())
print()

#Searching with Boolean in keyword.
#Caution: in is case-sensitive.
menu = "salad pasta sandwich pizza beverage dessert"
print("pizza" in menu)

#Create a function with def keyword.
#We can give it default values and return values.
#We can add if, else, pass into the function to make it more complicated.
#Escape sequence: \n \t \' \" \\ 
def chat(phrase1 = "Now", phrase2 = ", grow your own roses.", face = "N"):
    face = input("Are you happy or not? (Please input Y or N.)\n")
    if face == "Y":
        print(phrase1 + phrase2 + " XD")
    elif face == "N":
        print(phrase1.upper() + phrase2.upper())
        words = "Shut up! I know that."
        return words
    else:
        print("WJ Ker is a good mayor. Ha ha.")
        #or you can put pass here.
conversation = chat(student_name.upper())
print(conversation)

#Division generates a float number. Casting can change it back to int.
x = 3
y = 3
a = x/y
print(type(a))

#Two ways to do operations.
votes = 10
votes = votes + 1
print(votes)
votes -= 1
print(votes)

Comments

Popular posts from this blog

[申辦綠卡] EB-2 NIW 國家利益豁免綠卡申請教學(一)找合作的律師事務所

Image by  David Peterson  from  Pixabay

[申辦綠卡] EB-2 NIW 國家利益豁免綠卡申請教學(零)名詞解釋 Petitioner vs Applicant

Image by  David Peterson  from  Pixabay

[申辦綠卡] EB-2 NIW 國家利益豁免綠卡申請教學(二)閱讀官方申請條件—1

Image by  David Peterson  from  Pixabay