Introduction to Programming =========================== Introduction videos ------------------- Corey Schafer: - `Python Tutorial for Beginners 1: Install and Setup for Mac and Windows `_ - `Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data `_ - Integers and Floats - ``type()`` - Order of operations - Storiing a number in a variable - Incrementing a number - ``abs()`` - ``round()`` - Assignment operators - ``int()`` - `Visual Studio Code (Windows) `_ - `Visual Studio Code (Mac) `_ Mr. Gallo: - `Intro to Programming - Create and Run a Python File `_ - `Introduction - Storing Data in Variables `_ - `Introduction - Get Input from the User `_ Installing Python ----------------- Before you can start programming in Python, you will need the Python interpreter. The Python interpreter helps translate Python into `machine code `_. Please follow Corey Schafer's video `Python Tutorial for Beginners 1: Install and Setup for Mac and Windows `_. He will forward you to the `Python Download `_ page and show you what to do. You may also want to follow his other Python videos. They are all well done. An Integrated Development Environment (IDE) ------------------------------------------- When you watch the `previously mentioned video `_, Corey will recommend some IDEs like PyCharm and Atom. I suggest you get `VS Code `_ and its `Python Extension `_. It's currently the most popular IDE. Microsoft also has a `Getting Started with Python in VS Code `_ tutorial (optional). Corey Schafer also has a couple videos about VS Code specifically (optional viewing): - `Visual Studio Code (Windows) `_ - `Visual Studio Code (Mac) `_ Create and run a Python file ---------------------------- Video: `Intro to Programming - Create and Run a Python File `_ Output a message ---------------- .. code-block:: python print("Hello, World!") print("foo!") print("bar!") print("foo2", end="") # keep next print on same line print("bar2") Mathematical operations ----------------------- =================== ======== =========== ======= Operation Operator Example Result =================== ======== =========== ======= Addition ``+`` ``5 + 7`` ``12`` Subtraction ``-`` ``33 - 3`` ``30`` Multiplication ``*`` ``5 * 2`` ``10`` Division ``/`` ``5 / 2`` ``2.5`` Floor Division ``//`` ``5 // 2`` ``2`` Modulus (remainder) ``%`` ``56 % 10`` ``6`` Exponent ``**`` ``2**8`` ``256`` =================== ======== =========== ======= Order of operations ------------------- - **P**\arentheses (including function calls) - **E**\xponents - **M**\ultiplication - **D**\ivision - **A**\ddition - **S**\ubtraction .. code-block:: python 4 + 5 * 2 # 14 (4 + 5) * 2 # 18 round(4.25) * 3 # 12 2**8 * 2 # 512 Common Math Functions --------------------- Built-in math functions ^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python # Absolute value abs(-5) # 5 # General round round(5.55) # 6 round(5.45) # 5 round(3.141592653589793, 3) # 3.142 round(3.141592653589793, 2) # 3.14 From the math library ^^^^^^^^^^^^^^^^^^^^^ .. code-block:: python import math # square root math.sqrt(49) # 7 # Floor (round down) math.floor(5.99) # 5 # Ceiling (round up) math.ceil(99.1) # 100 Storing data in variables ------------------------- Video: `Introduction - Storing Data in Variables `_ .. code-block:: python :linenos: length = 5 width = 10 area = length * width print(area) # 50 Assignment Operators -------------------- The following operators will re-assign a variable value after performing a modification to the initial value. >>> num = 3 >>> num += 2 >>> num 5 >>> num *= 5 >>> num 25 >>> num -= 5 >>> num 20 >>> num /= 4 >>> num 5.0 .. note:: The special assignment operators are just short-hand for a mathematical operation and a re-assignment. .. code-block:: python num += 2 # is the same as num = num + 2 Get input from the user ----------------------- Video: `Introduction - Get Input from the User `_ .. code-block:: python name = input("Please enter a name: ") print("Hello", name) Convert strings to numbers -------------------------- .. code-block:: python :linenos: :emphasize-lines: 7,11 my_str = "hello" # obviously a string age_str = "50" # not-so-obviously a string. age_next_year = age_str + 1 # will NOT work. Cannot do math on a string. # we can convert strings that contain numbers into integers age_int = int(age_str) print(age_int + 1) # we can also convert into float (decimal) age_float = float(age_str) print(age_float + 1) # if receiving a number from user, convert it before storing it in a variable some_number = int(input("Please enter a number: ")) Format output text ------------------ There are a number of ways to format text by placing variable values into strings. All have their advantages and disadvantages. You may prefer to use different approaches in different situations. .. code-block:: python :linenos: :emphasize-lines: 5,8,11 name = "John" age = 37 # f-strings. Python 3.6+ message_fstring = f"Hello, {name}. You are {age} years old." # .format() message_dot_format = "Hello, {}. You are {} years old.".format(name, age) # concatenation message_concat = "Hello, " + name + ". You are " + str(age) + " years old." print(message_fstring) print(message_dot_format) print(message_concat) :: Hello, John. You are 37 years old. Hello, John. You are 37 years old. Hello, John. You are 37 years old.