Python for Babies
1 Installation
1.1 Python
Mac
Using Homebrew - general purpose. Suitable for people who want a basic package manager that works like a Linux package manager.
Using Anaconda - suitable for people interested in using python for data science.
Windows
Using Anaconda - suitable for people interested in using python for data science.
Linux
- Use package manager.
1.2 Visual Studio Code
- Choose the option for your environment from here
1.3 iPython
- If you downloaded python via the Anaconda distribution, ipython is already installed.
- Otherwise, you must install iPython via pip. Open your terminal and execute the following code:
For those with homebrew:
Otherwise:
2 Setting Up
- Open Visual Studio Code. You’ll see something like this:
2. Click on New File, and then Text File
- Click on Select Language
4. Input Python
- Hooray! We have a .py file ready to go! Now, before we do anything with this file, we want to open up an interactive terminal. Go to your toolbar and click on Terminal, then New Terminal:
- Now we have a terminal! But oh wait, it’s not running Python. Type in
- If you see a screen like the one below, you’re ready to go! Try typing in “print(‘hello world’)” into the terminal
3 Terminal vs. File
The difference between the interactive terminal and the file is that the interactive terminal takes in commands one by one, while the file reads the entire file once before evaluating commands. We’re gonna see how this works in a minute.
First, try typing in print(‘hello world’) into the ipython terminal. You should get something like this:
Now, let’s see what the file does. First, write the same command print(‘hello world’) in the file.
Next, save the file and give it the name “helloworld.py”. When you save the file, make sure to save it in a folder (aka directory) that you are familiar with. For example, I saved my file in “~/GDSC/python_for_babies/helloworld.py” where “GDSC” and “python_for_babies” are directories, or folders that lead to “helloworld.py”. If this concept is foreign to you, I’ll take some time to explain.
Now, go back to the ipython terminal. Unfortunately, we can’t call “helloworld.py” from inside the python terminal. We have to call it from our bash terminal. Type “quit()” to quit ipython, and direct yourself to the directory that contains “helloworld.py”. The following commands will help you
$ ls
# lists all the subdirectories and files in the current directory
$ cd (directory_name)
# navigates to (directory_name)Pay attention to how I use the two commands to find out where I am, and direct myself to the target directory.
YAY! We’re finally ready to run the file helloworld.py. Enter the following:
Now let’s try something a bit different. Go to the file and add a few lines:
What does this output?
This shows us that the file executes all the code contained in it, while the terminal just takes one command after another. Neat! Now we have the know-how of writing code in python with visual studio code.
4 Building Blocks
Now it’s time to actually start learning how to code!
4.1 Variables
In programming, we can assign a name to different data and store it in a convenient manner. These names are called variables. The python syntax for it is below:
now try printing the variable
## Hello, world!
Neat!
You can also do stuff like the following:
## 7
## 9
## 41
4.2 Data Types
Integers e.g. (1, 2, -4)
Floats (Real numbers, e.g., 1.3, 5.44, 2.0)
Strings (Characters encased in "" or '', e.g. “Hello world”)
Boolean (e.g., True, False)
4.2.1 Arithmetic
4.2.2 String Operations
4.3 Data Structures
There are many data structures in python, but for now we focus on lists.
Lists take the following form,
They are exactly what their name indicates, a list of datapoints.
We can access the elements of a list in the following way:
## 'b'
## 'c'
This is called indexing. But, oh no! The above commands are not returning the first and second elements of the list!
That is because in most programming languages, we always count from 0.
## 'a'
## 'b'
## 'e'
## 'd'
You can take slices of a list
## ['d', 'e']
## ['a', 'b', 'c']
## ['b', 'c']
Notice that the lower bound is inclusive, but the upper bound is not.
You can also treat strings like a list
## H
## !
## Hello
4.4 Control Flow Statements
4.4.1 For Loops
for loops are among the most basic operations in code.
They follow the following formula:
Let’s look at a few examples.
## The price is 10
## The price is 25
## The price is 5
## The price is 70
## The price is 10
prices = [10, 25, 5, 70, 10]
tax = 0.10
for p in prices:
p = p * (1 + tax) # overwriting value of p
print "The price with tax is", p## The price with tax is 11.0
## The price with tax is 27.5
## The price with tax is 5.5
## The price with tax is 77.0
## The price with tax is 11.0
What if we want to know the total amount spent?
prices = [10, 25, 5, 70, 10]
tax = 0.10
total = 0 # total begins with 0
for p in prices:
p = p * (1 + tax)
total += p # increment total by p
print "The price with tax is", p## The price with tax is 11.0
## The price with tax is 27.5
## The price with tax is 5.5
## The price with tax is 77.0
## The price with tax is 11.0
## The total price is 132.0
4.4.2 Conditionals
Conditionals take the following form:
## a is less than or equal to 5
But wait, what if I want to know if \(a\) is exactly equal to 5?
a = 5
if a > 5:
print("a is greater than 5")
elif a == 5: # we call this an else-if or if-else statement
print("a is exactly equal to 5")
else:
print("a is less than 5")## a is exactly equal to 5
Let’s combine this with a for loop:
prices = [10, 25, 5, 70, 10]
tax = 0.10
for p in prices:
if p >= 20:
p = p * (1 + tax) # overwriting value of p
else:
p = p
print "The price with tax is", p## The price with tax is 10
## The price with tax is 27.5
## The price with tax is 5
## The price with tax is 77.0
## The price with tax is 10
But, \(p = p\) is a redundant statement! Just take that out.
prices = [10, 25, 5, 70, 10]
tax = 0.10
for p in prices:
if p >= 20:
p = p * (1 + tax) # overwriting value of p
print "The price with tax is", p## The price with tax is 10
## The price with tax is 27.5
## The price with tax is 5
## The price with tax is 77.0
## The price with tax is 10
Same result! What if we want the total price of just taxable products?
prices = [10, 25, 5, 70, 10]
tax = 0.10
total_taxable = 0
for p in prices:
if p >= 20:
p = p * (1 + tax) # overwriting value of p
total_taxable += p
print "The price with tax is", p## The price with tax is 10
## The price with tax is 27.5
## The price with tax is 5
## The price with tax is 77.0
## The price with tax is 10
## The total of taxable products is 104.5
How about the total of nontaxable products?
prices = [10, 25, 5, 70, 10]
tax = 0.10
total_taxable = 0
total_nontaxable = 0
for p in prices:
if p >= 20:
p = p * (1 + tax) # overwriting value of p
total_taxable += p
else:
total_nontaxable += p
print "The price with tax is", p## The price with tax is 10
## The price with tax is 27.5
## The price with tax is 5
## The price with tax is 77.0
## The price with tax is 10
## The total of taxable products is 104.5
## The total of nontaxable products is 25
5 Exercise
How to test your code:
Once you’ve cloned the git repository and opened exercise1.py, go to the terminal and enter the following code:
6 Programming Assignment
Acknowledgment: This programming assignment was originally designed by Emma Nechamkin, inspired by the book Networks, Crowds, and Markets. It was furthered developed by the faculty of University of Chicago, whose specific names are cited in the source files. Additionally, modifications were made by Jake Underland.