--- tags: Python Workshop 沈煒翔 --- # Lesson 1: Environment + Knowledge + Data type + Operators We need to first install the environment to develop Python. Anaconda is a very popular Python environment management system. It helps us manage multiple Python environments on the same computer. (We would only use one environment in this course though). Download Anaconda here: https://www.anaconda.com/products/distribution ## Knowledge ### Popular Programming Languages * JavaScript/TypeScript: server, webpage, APP (all purpose) * Python: server, data (all purpose) * Matlab: used in academic (not recommended, should use Python) * C: safety, hardware-related (e.g. car systems, micro-chips ...) * C++: performance beast (e.g. 3A games, visual rendering) .... and so on, there is 100+ languages out there Luckily, most programming languages share the exact same concept, so it is easy to transition to other lanauges once you master one of them. ### Interpreted Language vs Compiled Language Interpreted Language (直譯語言) * Fast to write, but slow to execute * Write -> Execute * Python, Matlab, Javascript * Most of the time you want to use this >Alvin: Instead of generating (writing down) an object code, interpretor will visit each part of the code and translate them to machine language for machine one by one. >->save more time if you run the source code in the first time Compiled Language (編譯語言) * Slow to write, but fast to execute * Write -> Compile -> Execute * C, C++, Java >Alvin: The compiler read the code from the file all at once and generate an object code written in machine language. (faster for execution after having the object code, but slower if you need to modify the source code frequently such as machine learning. I guess the same object code will be used if there is no change in the source code) >->execute faster if you do not need to modify the code ### Static Typed vs Dynamic Typed Static Typed * You need to explicitly tell the computer what type is each variable * You need to always keep typing in mind ```cpp // This is C++ code int num = 1; // num is an integer and set it to one float num2 = 2.5; // num2 is a floating number ``` The gray words are "comments". It is for humans to see and machines would ignore that. Dynamic Typed * The interpreter figures the typing automatically * You forget everything about typing * Most of the time you want to use this ```python # This is Python code num = 1 # interpreter would think this is integer num2 = 2.5 # interpreter would think this is floating ``` The "=" operator we saw is "assignment" - assign something to something. ("=" is not the meaning of equal) ### Common operators ``` = assignment + addition - subtraction * multiplication / division ** power ``` ## The Spyder IDE IDE: integrated development environment * basically a friendly place for you to "develop" your code * not needed for execution * nowadays IDE is a must for programming 1. Editor: place for editting 2. Variable Explorer: to see all variables 3. Interactive console: place for execution ## Data type Basic data types in Python | Data type | Examples | Operators | |------------------------|------------------------|----------------| | Integer (int) | 0, 1, -3, 1000 | +, -, *, / | | Floating point (float) | 0.0, 5.3, 2e10 |+, -, *, / | | String (str) | 'hello', "Hi", "12345" | | We can use `type()` to check the data type of the variable ```python a = 5 print(type(a)) #>>> <class 'int'> b = 15e4 # It means 15 with 4 zeros afterward. print(b) #>>> 150000.0 print(type(b)) #>>> <class 'float'> c = a + 15.3 # Automatically cast to float. Just like real Math. print(c) #>>> 20.3 print(type(c)) #>>> <class 'float'> d = 1 + 5 * 2 # Multiply/divison first, then addition/subtration. Just like proper Math. print(d) #>>> 11 print(type(d)) #>>> <class 'int'> e = 50 / 3 # Also cast to float, like Math. print(e) #>>> 16.666666666666668 # some error caused by the precision limit of floating numbers. print(type(e)) #>>> <class 'float'> f = 1 + 2 * 3 ** 2 # behaves like Math print(f) #>>> 19 print(type(e)) #>>> <class 'int'> ``` You can also **forcely** change the data type of a variable using casting. Python is a dynamic type language which means programmers should not worry too much about the typing. Note: When you are using casting, you are probably using Python incorrectly. In Python, we don't need to differentiate "int" and "float" to much, just treat them as "numbers". ```python old_score = 54 new_score = old_score ** 0.5 * 10 print("New score is", new_score) # display in console ``` ### Exercise Calculate and print the how much tax you have to pay. ```python tax_ratio = 0.15 earnings1 = 100 earnings2 = 50 ``` Given the diameter of a cirlce, calculate its area. ```python diameter = 10 print(78.5398) ``` Given the height, weight of a person, calculate the person's BMI. BMI = weight (in kg) / height^2 (in m) BMI = 體重(公斤) / 身高^2 (公尺) ```python weight = 100 # in kg height = 170 # in cm print(34.6020) ```