# Session 1 : Python 101 ###### tags: `CS Projects Class` --- ## :exclamation: Session 1: Python 101 ##### :exclamation::exclamation::exclamation:Log into your ***Google Colaboratory***:exclamation::exclamation::exclamation: --- ### Hello, World! - In <span style="font-family:Papyrus; color:red; font-style: italic; font-style: bold; font-size:1.5em">C</span>, your code should look like this: ``` main(){ printf("hello, word") } ``` --- - In <span style="font-family:Papyrus; color:red; font-style: italic; font-style: bold; font-size:1.5em">Python</span>, your code should look like this: ``` print("hello, world") ``` --- ![](https://i.imgur.com/7JB2LQw.png) --- ### Variables and simple data types #### Naming variables - Be sure to keep the following rules in mind: - Variable names can contain only *letters, numbers, and underscores*. They can start with a letter or an underscore, but not with a **number**. For instance, you can call a variable *message_1* but not *~~1_message~~*. --- - **Spaces** are not allowed in variable names, but **underscores** can be used to separate words in variable names. For example, *greeting_message* works, but *greeting message* will cause errors. --- - Avoid using Python keywords and function names as variable names. We will learn about Python built-in functions later. --- Python Keywords | Python built-in functions :-------------------------:|:-------------------------: ![](https://i.imgur.com/LY7zTef.png) | ![](https://i.imgur.com/T0nL3bo.png) --- - Variable names should be short but descriptive. For example, *name* is better than *n*, *student_name* is better than *s_n*, and *name_length* is better than *length_of_persons_name*. --- #### :boom: Try it yourselve! 1. *Simple Message*: Store a message in a variable, and then print that message. 2. *Simple Messages*: Store a message in a variable, and print that message. Then change the value of your variable to a new message, and print the new message. --- ### Basic Data Types 1. String 2. Integer 3. Float ``` >>> print(7+8) 15 >>> print("hello" + "world") hello world ``` --- - If you tell your computer to mix these two different data types, your computer is not going to know what to do and will raise an error. ![](https://i.imgur.com/I7lz4V6.png) --- ## :boom: Read the errors carefully, understand what they're telling you, and then use that new knowledge to help you fix the mistake. --- ![](https://i.imgur.com/I7lz4V6.png) - This *TypeError* tells us that the plus sign can't be used between an *int* type and a *str* type, which are short names for integer and string. --- ### Debugging skills :boom: copy the error and paste into the search bar and *voilĂ *: <span style="font-family:Papyrus; color:red; font-style: italic; font-style: bold; font-size:1.5em">Stack Overflow</span> --- #### :boom: Try it yourselve! - What is 7 + 8 equals in Python? - What is "7" + "8" equals in Python? - What is 7 + 8.5 equals in Python? --- Behind the scene, the computer is busy automatically converting our *integer* seven into a *float* seven. This lets Python then add together the values to return results that is also a *float*. --- ### Type Conversions In Python, we can easily convert a number into a string, and a string into a number. :boom: ***Try it yourself:*** ``` print(type(int(4))) print(type(int("a"))) print(type(float(4))) print(float(4)) print(type(str(4))) ``` --- ### *type* function ``` >>>print(type("a")) <class 'str'> >>>print(type(2)) <class 'int'> >>>print(type(2.5)) <class 'float'> ``` --- But be aware that you can sometimes get an arbitrary number of decimal places in your answer: ``` >>>0.2 + 0.1 0.30000000000000004 >>>3 * 0.1 0.30000000000000004 ``` --- This happens in all languages and is of little concern. Python tries to find a way to represent the result as precisely as possible, which is sometimes difficult given how computers have to represent numbers internally. :skull: Just ignore the extra decimal places for now. --- ### Variables - When we ask a computer to perform an operation for us, we usually need to store values and give them names so that we can refer to them later. - Those values can be of any data type; numbers, strings or even the results of operations. - In Python, declaring a variable is ***EASY*** :banana: --- ### Assignment ``` >>>length = 10 >>>width = 2 >>>area = length * width >>>print(area) 20 ``` This is called assignment. Here we assign the *length variable* the value of **10**. We assign the *width variable* the value of **2** and we assign the *area variable* with the **result of the expression length times width**. --- ### Expression An expression is a combination of numbers, symbols or other variables that produce a result when evaluated. --- ### :boom: ***Try it yourself:*** ``` >>>print("The area of the triangle is: " + str(area)) ``` There are many other ways to print this string, and we will dive into this later. --- ### Operations Example | Meaning | Results :---------:|:---------:|:---------:| a + b | addition | sum of a+b | a - b | subtraction | b subtracted from a | a * b | multiplication | product of a and b | a / b | division | quotient when a is divided by b; the result always has type *float*| --- Example | Meaning | Results :---------:|:---------:|:---------:| a % b | modulo | remainder when a is divided by b | a // b | floor division (also called integer division) | quotient when a is divided by b, rounded to the ***next*** smallest whole number | a ** b | exponentiation | a raised to the power of b | --- ### :speech_balloon: What is the difference between ***floor*** and ***round*** function in Python? --- ### :fire: Homework: - Create a simple Python scripts calculating the solutions of a quadratics function, with the coefficients a, b, c is typed in by the user. - *(Optional)* Build a cheat sheet for this built-in Python library [**Math**](https://docs.python.org/3/library/math.html), you can use this library by *import* a library, with we will talk about in the next class. ``` import math ``` --- ![](https://i.imgur.com/acKURBN.png) --- ## This is the end of our first session. --- # Introducing capstone projects ideas --- ## Budget management app (BIG PROJECT) --- ## Meeting setting app (BIG PROJECT) --- ## AI - educating website (SMALL - MEDIUM PROJECT) --- ## Invidual Website --- ## Create presentations using Markdown - auto convert Markdown to pptx or pdf --- ## Algorithms visualizer with PyGame --- ## Classpage --- Read Nga's [stories](https://spiderum.com/bai-dang/Minh-17-tuoi-va-minh-dang-lam-gi-voi-Tri-tue-nhan-tao-8YEtyP60lAKB) on Spiderium!
{"metaMigratedAt":"2023-06-16T23:02:40.292Z","metaMigratedFrom":"Content","title":"Session 1 : Python 101","breaks":true,"contributors":"[{\"id\":\"c7c0a657-74ce-4c87-8beb-3ed25f434ff0\",\"add\":7287,\"del\":574}]"}
    260 views