跳到主要內容

Kaggle 第一課




Intro
This course covers the key Python skills you’ll need so you can start using Python for data science. The course is ideal for someone with some previous coding experience who wants to add Python to their repertoire or level up their basic Python skills. (If you're a first-time coder, you may want to check out these "Python for Non-Programmers" learning resources.)
We'll start with a brief overview of Python syntax, variable assignment, and arithmetic operators. If you have previous Python experience, you can skip straight to the hands-on exercise.
這堂課程涵蓋了你所需要的Python關鍵技巧,
這樣你就可以利用Python開始進行數據科學了。
這堂課程適合之前有寫過程式又想學Python的人。
(如果你第一次寫程式,請參考這篇"Python for Non-Programmers"的說明)
<< Bon: 這篇是Python官方網站介紹可以直接網頁全中文化 >>
我們會先簡介一下Python的語法、變數指定、算術運算子。
如果你之前寫過Python,你可以直接跳到練習題。

Hello, Python!
Python was named for the British comedy troupe Monty Python,so we'll make our first Python program an homage to their skit about Spam? Just for fun, try reading over the code below and predicting what it's going to do when run. (If you have no idea, that's fine!) Then click the "output" button to see the results of our program. 
Python是用英國的一位喜劇演員Monty Python命名的,
所以我們就用一個關於垃圾郵件的笑話來開始做第一個Python程式吧。
試讀下面的程式碼,然後預測運行的結果。
(如果你想不出來也沒關係) 然後點output看結果


There's a lot to unpack here! This silly program demonstrates many important aspects of what Python code looks like and how it works. Let's review the code from top to bottom.
這裡有很多關卡需要解鎖。
這個中二程式會展示Python許多重要的觀念和運作模式。
我們從頭到尾來看一下這些程式碼吧。


Variable assignment:
Here we create a variable called spam_amount and assign it the value of 0 using =, which is called the assignment operator.
變數指定:我們在這裡創造了一個變數叫做spam_amount
並且用"="把它的數值指定為0
"="就是所謂的指定運算子

Aside: If you've programmed in certain other languages (like Java or C++), 
you might be noticing some things Python doesn't require us to do here:
we don't need to "declare" spam_amount before assigning to it.
we don't need to tell Python what type of value spam_amount is going to refer to. In fact, we can even go on to reassign spam_amount to refer to a different sort of thing like a string or a boolean.
偷偷說:如果你用過其他程式語言,
你可能會注意到有些事情Python不需要做:
我們不需要宣告spam_amount
我們不需要告訴Python  spam_amount是哪一種數值。
我們甚至可以重新指定spam_amount去做不同東西像是字串或布林。


Function calls: print is a Python function that displays the value passed to it on the screen. We call functions by putting parentheses after their name, and putting the inputs (or arguments) to the function in those parentheses.
函數呼叫:print是一個顯示數值的函數。
我們在函數後面放小括號來呼叫函數,把需要的內容放在括號裡。

The first line above is a comment. In Python, comments begin with the # symbol.
第一行是註解。Python#來註解。

Next we see an example of reassignment. Reassigning the value of an existing variable looks just the same as creating a variable - it still uses the = assignment operator.
接下來我們可以看到重新指定的示範。
重新指定一個已存在變數和創造一個變數的方式一樣,
只要用"="就可以。


In this case, the value we're assigning to spam_amount involves some simple arithmetic on its previous value. When it encounters this line, Python evaluates the expression on the right-hand-side of the = (0 + 4 = 4), and then assigns that value to the variable on the left-hand-side.
在這個示範中,我們指定給spam_amount的數值包含了ㄧ些簡單的數學運算。
在這行程式碼中,Python計算出算式右邊 (0 + 4 = 4)
然後再把數值指定給左邊的變數。

We won't talk much about "conditionals" until later, but, even if you've never coded before, you can probably guess what this does. Python is prized for its readability and the simplicity.
我們等等才會討論條件句,不過即使你以前都沒寫過程式,
你應該還是可以猜得出來才對。
Python就是以易讀和簡單出名的。

Note how we indicated which code belongs to the if. "But I don't want ANY spam!" is only supposed to be printed if spam_amount is positive. But the later code (like print(viking_song)) should be executed no matter what. How do we (and Python) know that?
留意有if的那行程式碼。
如果spam_amount是正數才會印出But I don't want ANY spam!
不過後面的程式碼像print(viking_song)不管怎麼樣都會執行。
我們和Python怎麼知道的?

The colon (:) at the end of the if line indicates that a new "code block" is starting. Subsequent lines which are indented are part of that code block. Some other languages use {curly braces} to mark the beginning and end of code blocks. Python's use of meaningful whitespace can be surprising to programmers who are accustomed to other languages, but in practice it can lead to more consistent and readable code than languages that do not enforce indentation of code blocks.
if這一行結尾的分號(:)是在說明要開始一段新的程式碼區塊
下面縮排的那幾行代表它們是區塊的一部分。
有些程式語言用{大括號}來標示區塊的開始和結束。
Python這種有意義的空白可能會讓習慣其他語言的工程師覺得很驚訝,
實務上這樣比較一致性而且易讀。

The later lines dealing with viking_song are not indented with an extra 4 spaces, so they're not a part of the if's code block. We'll see more examples of indented code blocks later when we define functions and using loops.
後面處理viking_song的這幾行沒有4格空白的縮排,
所以它們不屬於if的區塊。
後面我們要處理函數和使用迴圈的時候,
我們會看到更多縮排的例子。

This code snippet is also our first sighting of a string in Python:
"But I don't want ANY spam!"
這段程式碼片段是我們第一次在Python裡看到字串

Strings can be marked either by double or single quotation marks. (But because this particular string contains a single-quote character, we might confuse Python by trying to surround it with single-quotes, unless we're careful.)
字串可以用一個或兩個上引號標示。
(不過因為這行字串包含了放在單引號的字元,
如果不小心又用了單引號,
會讓Python搞不清楚到底是哪一種)

The * operator can be used to multiply two numbers (3 * 3 evaluates to 9), but amusingly enough, we can also multiply a string by a number, to get a version that's been repeated that many times. Python offers a number of cheeky little time-saving tricks like this where operators like * and + have a different meaning depending on what kind of thing they're applied to. (The technical term for this is operator overloading)
”*” 這個運算子可以用來乘兩個數字(3*3等於9)
不過我們也可以用字串乘數字來讓它重複出現。
Python提供了一些搞笑的省時小把戲,
”*””+”這類運算子可以在不同情境有好幾種不同用途。
(這個方式的技術用語叫做運算子重載)

Numbers and arithmetic in Python (Python中的數字和計算)
We've already seen an example of a variable containing a number above:
我們在上面看過包含數字的變數了

"Number" is a fine informal name for the kind of thing, but if we wanted to be more technical, we could ask Python how it would describe the type of thing that spam_amount is:
數字是非正式的名稱,如果我們想要更專業,
我們可以問Python怎麼描述spam_amount的類型

It's an int - short for integer. There's another sort of number we commonly encounter in Python:
這是個”int”-整數的縮寫。我們在Python裡常遇到另一種數字

A float is a number with a decimal place - very useful for representing things like weights or proportions.
浮點是帶有小數點的數字-在說明重量或比例時很有用

type() is the second built-in function we've seen (after print()), and it's another good one to remember. It's very useful to be able to ask Python "what kind of thing is this?".
type() 是我們看過的第二個內建函數,很值得記起來,
想問Python這是哪一種東西時很有用。

A natural thing to want to do with numbers is perform arithmetic. We've seen the + operator for addition, and the * operator for multiplication (of a sort). Python also has us covered for the rest of the basic buttons on your calculator:
想處理數字自然而然就要做算術。
我們看過運算子”+”做加法,”*”做乘法,當然也可以做計算機上的其他基本運算。


One interesting observation here is that, whereas your calculator probably just has one button for division, Python can do two kinds. "True division" is basically what your calculator does:
你的計算機可能只能做一種除法,不過Python可以做2種。
"True division"就是你的計算機會做的那種。

It always gives us a float.
The // operator gives us a result that's rounded down to the next integer.
它永遠會出現浮點。
”//”這個運算子只給整數。

Can you think of where this would be useful? You'll see an example soon in the coding challenges.
你有想到用在哪裡比較好嗎?做下面的練習你就知道了。

Order of operations (運算子的順序)
The arithmetic we learned in primary school has conventions about the order in which operations are evaluated. Some remember these by a mnemonic such as PEMDAS - Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.
Python follows similar rules about which calculations to perform first. They're mostly pretty intuitive.
我們在國小就學過算術的固定順序了。
有些人用口訣來記PEMDAS – 括號、指數、乘除、加減。
Python也用相同的規則。

Sometimes the default order of operations isn't what we want:
有時候運算的預設順序不是我們想要的

Parentheses are your useful here. You can add them to force Python to evaluate sub-expressions in whatever order you want.
小括號在這裡就很有用。你可以加入小括號改變算式。

Builtin functions for working with numbers (內建的算術函數)
min and max return the minimum and maximum of their arguments, respectively...
“min””max” 分別會出現參數中的最小值和最大值。

abs returns the absolute value of it argument:
“abs” 會出現參數的絕對值

In addition to being the names of Python's two main numerical types, int and float can also be called as functions which convert their arguments to the corresponding type:
“int”“float”除了是主要的數值類型名稱之外,它們也可以當作函數用。

Your Turn 輪到你了



留言

這個網誌中的熱門文章