Welcome to the Basic Python Course

Designed by Lucas Liu and Suyang Lo, this is a very short introductory course to Python for participants that have little experience with programming or are unfamiliar with Python.

Installation Instructions

Installation for Windows

Installation for macOS

All downloads for other operating systems can be found here.

Variables

Variables are reserved memory locations to store values. The declaration of a variable occur automatically when a value is assigned to a variable with the equal sign (=). Values stored in variables can be changed through operations such as +, -, * and /.

v = 1
v = v + 1
v += 1
                    

Multiple variables can also be assigned in different ways. The following code declares all a, b and c, assigning a value of 1 to each.

a = b = c = 1
                    

Another way of assigning multiple variables is shown below:

a, b, c = 1, 2, 'hello'
                    

The function print() allows variables to be outputted to the screen:

a = 'Hello World!'
print(a)
                    

Conditions are often used to compare variables. The following code illustrates the most common comparison operators and their expected output:

3 == 4 # False
3 != 4 # True
3 <  4 # True
3 <= 4 # True
3 >  4 # False
3 >= 4 # False
                    
Lists

A list is a group of variables. Items are separated by commas and enclosed by square brackets ([]). They are similar to arrays but data belonging to the same list can be of different data types. To following code declares a list with 4 elements that have mixed data types:

list = ['abc', 123, 'hello', 22]
                    

Data stored at different positions in a list can be accessed using [index]. This is demonstrated in the example below. In Python, lists are 0-indexed, meaning that the first element has an index of 0, the second an index of 1, the third an index of 2 etc..

list = ['abc', 123, 'hello', 22]
print(list[0]) # This should print out 'abc'
print(list[1:3]) # This should print out the 2nd and 3rd elements. Note that the last element we note down (3)is not printed.
print(list[1:]) # This prints out every item in the list starting from the 2nd element.
                    

Lists can be 'added' together (this is called concatenation) like this:

concatenated_list = ['a', 1] + ['b', 2]
                    
Conditional Statements

These are used to execute different code depending on different conditions. "else" is often used if none of the listed if cases are satisfied.

They are written out as following:

a = 50
if a == 50:
  print('a is 50')
elif a < 50:
  print('a is less than 50')
else:
  print('a is greater than 50')
                    

Remember that indentations matters in Python. Usually, you can indent statements located inside the if, if-else, or if-elif-else block using tabs, 4 spaces or 2 spaces.

AND and OR statements can be used with our conditional statements. When you use AND to combine two boolean expressions, it will only return True if both expressions are true. When you use OR to combine two boolean expressions, it will only return False if both expressions are false.

Iteration

Iteration is basically repeating a process over different values in a list. For loops can be used to repeat code for a given amount of times. Remember that for loops start counting from zero. The following code prints out 0, 1 and 2.

for count in range(3):
  print(count)
                    

For loops can also be used to iterate through lists. In fact, range(3) actually returns a list for the for loop to iterate through.

for letter in ['a', 'b', 'c']:
  print(letter)
                    

Iteration can also be achieved through while loops. This type of loop repeats code while a condition remains true. It can be used alongside 'else' to execute code if the condition is no long true.

a, b = 3, 0
while b < a:
  b = b + 1
  print(b)
else:
  print('b is no longer less than a')
                    

The output of the code above should be:

1
2
3
b is no longer less than a
                    
Exercise 1

Output a list of n values in the fibonnaci sequence (The fibonnaci sequence is a mathematical sequence where the next term is equal to the sum of the previous 2 terms, beginning with 1,1. First six values are 1,1,2,3,5,8)

Exercise 2

Create an algorithm that checks if a number is a prime number (0 and 1 are not prime numbers)

Exercise 3

Write a program that guesses the number between 1 and 100 that you are thinking of (try not to just make it guess all numbers from 1 to 100)

Exercise 4

Use splitting strings to reverse the order of a string.

str = "this is a string"
# This method splits the string every time it finds a space
split = str.split()
# The below method joins all the strings with spaces
print("".join(str))
                    

This is the output of the above code:

this is a string
                    
Exercise 5

Create a password generator using the random library that will generate a password of random length between 9 and 15 characters that uses letters, numbers, and symbols. (Do a bit of research if you don't know how to use the random library)