Run in Google Colab
|
View on GitHub
|
Module 1 - Python Basics 1¶
Are you still new to Python? Or your python skill is a bit rusty?
Then, feel free to use this to learn or refresh Python basics! You don't have to submit this notebook. It is just for your own reference.
We begin by focusing on constructing and executing Python scripts. This fundamental skill allows you to instruct your computer to perform a multitude of tasks. As you master this technique, you will be able to transform abstract ideas into executable programs, opening up a world of possibilities for automation and efficient problem-solving.
Moving forward, we will dive into loops and conditional statements. These powerful constructs enable your programs to dynamically react to varying scenarios and diverse data inputs. Leveraging these tools allows for the creation of responsive and adaptive code, which is essential for tackling complex challenges.
Towards the end of the module, we'll explore the concept of reusable functions. Crafting well-structured functions not only enhances the modularity and reusability of your code, but also simplifies maintenance. This skill is paramount for reducing redundancy, streamlining debugging, and ensuring that your code remains clear and adaptable.
Objectives¶
- Construct and Execute Python Scripts: Develop the ability to write and run basic Python scripts that adhere to standard syntax, establishing a firm foundation in programming.
- Apply Loops and Conditional Statements: Implement loops and conditional constructs to manipulate data effectively and solve intricate problems.
- Create Reusable Functions: Design and integrate Python functions that promote modularity and reusability, enhancing both code efficiency and maintainability.
The topics covered in this module:¶
- Variables and built in datatypes
- Printing and user input
- Operators
- Lists and Tuples
- Dictionaries
- Sets
- Flow control
1. Variables and built-in data types¶
1.1 Basic and variables¶
You can comment in Python using #. Comments are ignored by the Python interpreter.
To define a variable, assign a value to a variable name. Python will automatically detect the data type. The syntax is variable_name = value. You can print a variable using the print(variable_name) function.
# This is a comment and will be ignored by the interpreter
a_variable = "the value"
# This is another comment and will also be ignored by the interpreter
print(a_variable)
Variables can be reassigned with a different value and data type. For example, you can change an integer to a string:
a_variable = 10
print(a_variable) # Output: 10
a_variable = "Now I'm a string!"
print(a_variable) # Output: Now I'm a string!
Q: Create two variables and then swap their values. Print the variables before and after the swap.
- For example if
a = 1andb = 2, after the swapa = 2andb = 1.
# TODO: Write your code here
1.2 Printing composed variables and text¶
Use the print() function to print variables and text. You can use the + operator to concatenate strings. You can also concatenate variables and strings by separating them with a comma.
print("Hello, World!") # Output: Hello, World!
# Example of print with concatenation
a_planet = "Earth"
print("Hello from " + a_planet + "!")
# Output: Hello from Earth!
# Example of print with comma separated values
a_planet = "Mars"
another_planet = "Jupiter"
print("Hello from", a_planet, "and", another_planet + "!")
# Output: Hello from Mars and Jupiter!
Another way to format text is using the f-string method. You can use f-string by adding an f before the string and then use curly braces {} to insert variables.
# Example of f-string print
a_planet = "Earth"
another_planet = "Mars"
print(f"Hello from {a_planet} and {another_planet}!") # Output: Hello from Earth and Mars!
1.3 Int and Float¶
Numbers can be stored in variables. Integers are whole numbers, while floats are numbers with decimal points. Example: x = 5 or y = 3.14.
# Declaring an integer and a float variable
x = 10
y = 3.0
k = x + y + 100
# The value stored in the variable can be inspected by using print statement.
# Type of a variable var can be checked by calling type(var)
print("The value of x is", x, "and it is of type", type(x))
# f-strings can be used to write more elegant print statement.
print(f"The value of y is {y} and it is of type {type(y)}")
print(f"The value of k is {k} and it is of type {type(k)}")
# casting int to float
print(f"x is of type {type(x)}")
x = float(x)
print(f"x is of type {type(x)} after casting")
Numbers can also be written in different formats. For example, you can use scientific notation: z = 1.23e-4 (equivalent to 0.000123).
# Numbers can also be written in scientific notation
x = 1.05e6
print(f"x is {x} and it is of type {type(x)}")
# You can also use _ to make the number more readable
x = 1_000_000
print(f"x is {x} and it is of type {type(x)}")
You can perform arithmetic operations on numbers. The basic arithmetic operators are +, -, *, /, // (integer division), and % (modulo).
# Arithmetic operators
# Addition
z = x + y
print(f"Adding x and y gives {z}")
# Subtraction
z = x - y
print(f"Subtracting y from x gives {z}")
# Multiplication
z = x * y
print(f"Multiplying x and y gives {z}")
# Division
z = x / y
print(f"x divided by y gives {z}")
# Floor Division
z = x // y
print(f"x divided by y gives {z} as quotient")
# Modulus Operator
z = x % y
print(f"x divided by y gives {z} as reminder")
# Exponentiation
z = x ** y
print(f"x raised to y gives {z}")
# self increment by 1
x = x + 1
# This is equivalent to x += 1
print(f"x + 1 gives {x}")
1.4 Booleans and None¶
Bolean values are True and False and are used to represent binary values. None is a special value that represents the absence of a value, when tested, normally it results to False.
# True and False are the key words that represent bool values in python
a = True
b = False
print(f"a is {a} and b is {b}")
print(f"Type of variable a and b is {type(a)}")
# None in python represents the absence of something; similar to null value
c = None
print(f"c is {c} and is of type {type(c)}")
# Any non-zero integer value is true and zero is false.
# Also anything with a non-zero length is true and empty sequences are false.
You can use logical operators and, or, and not to combine boolean values.
# logical operators
# and, or and not operate on bool variables
# OR operator: Gives True when either of the expressions evaluates to True
# expr1 or expr2
print(f"a or b is {a or b}")
print(f"a or a is {a or a}")
print(f"b or b is {b or b}")
# AND operator: Gives True when both the expressions evaluates to True
# expr1 and expr2
print(f"a and b is {a and b}")
print(f"a and a is {a and a}")
print(f"b and b is {b and b}")
# NOT operator: negates a bool
# not expr1
print(f"Not of a is {not a}")
Comparison operators result in boolean values. The comparison operators are == (equal), != (not equal), >, <, >=, and <=.
You can also combine comparison operators to create more complex conditions, such as x > 5 and x < 10, which checks if x is between 5 and 10.
Additionally, logical operators can be used in conjunction with comparison operators to create more complex conditions, for example: if x > 5 and x < 10: print("x is between 5 and 10").
# comparison operators
x = 10
y = 3.0
z = 5
# greater that, less than, greater than equal to and lesser than equal to
x > y
x >= y
x < y
x <= y
# equals and not equals
x == y
x != y
# Chained Expressions
x > y > z
(x > y) or (x > z)
1.5 Strings¶
Strings are sequences of characters, e.g., pieces of text. You can create them by enclosing characters in quotes. Python treats single quotes the same as double quotes.
Strings can be concatenated using the + operator. You can also multiply strings by integers to repeat them.
You may need to escape special characters in strings using a backslash \. For example, to include a quote in a string, you can use \'.
You can use the len() function to get the length of a string.
# strings are represented using single or double quotes
first_name = "Adam"
last_name = 'Eve'
# \ is used to escape characters
middle_name = 'zero\'s'
# string concatenation
full_name = first_name +' ' + middle_name + ' ' + last_name
print(f"Full name is {full_name}")
print(f"Full name is of type {type(full_name)}")
# length of string
length = len(full_name)
print(f"Length of full name is {length}")
Strings can be indexed and sliced similar to lists and tuples.
List indexing is discussed in the list section
You can 'multiply' a string by an integer to repeat it.
repeated_string = "Hello " * 10
print(f"repeated_string is {repeated_string}")
Some characters have special meanings in strings. For example, \n represents a newline character, and \t represents a tab character. You can use r before the string to treat it as a raw string, ignoring special characters.
# characters with special meaning
# \n - newline
# \t - tab
# \\ - backslash
# examples
print("Hello\nWorld")
print("Hello\tWorld")
print("Hello\\World")
Casting is the process of converting one data type to another. You can cast strings to integers or floats, and vice versa. For example, you can convert a string to an integer using int("5").
# casting str to int
total = int('1') + int('2')
print(f"The value of total is {total} and it is of type {type(total)}")
# casting int to str
total = str(1) + str(2)
print(f"The value of total is {total} and it is of type {type(total)}")
# empty string is considered as False
empty_string = ''
bool(empty_string)
To replace a part of a string, you can use the replace() method. To split a string into a list of substrings, you can use the split() method.
# replace method
s = "Hello, World!"
s = s.replace("World", "Universe")
print(s)
Note that the result of the f-string composition is a string.
# f-string composition is a string
helloworld = f"Hello, World!"
full_sentence = f"This is a full sentence. {helloworld} and it is of type {type(helloworld)}"
print(full_sentence)
Q: Create a sentence about the weather using variables for the temperature and the weather condition.
- Print the sentence
- Example: "The temperature is 25 degrees and it is sunny."
- Then use replace to change the weather condition to "cloudy" and print the new sentence.
# TODO: Write your code here
1.4 Lists and Tuples¶
Lists are collections of items that are ordered and changeable. Tuples are collections of items that are ordered and unchangeable.
They can contain any type of object, even other lists or tuples. They can be used for instance to store a sequence of numbers, strings, or a mix of both.
# Arrays are implemented as lists in python
# creating empty list
names = []
names = list()
# list of strings
names = ['Zach', 'Jay']
print(names)
# list of intergers
nums = [1, 2, 3, 4, 5]
print(nums)
# list of different data types
l = ['Zach', 1, True, None]
print(l)
# list of lists
ll = [[1, 3], [2, 3], [3, 4]]
# finding the length of list
length = len(l)
print(length)
You can repeat a list or tuple by multiplying it by an integer.
# You can repeat a list or tuple by multiplying it by an integer.
l = [1, 2, 3] * 3
print(l)
t = (1, 2, 3) * 3
print(t)
You can change the content of a list by assigning new values to its elements. You can also add elements to a list using the append() method or extend a list with another list using the extend() method.
# Lists are mutable
names = names + ['Ravi']
names.append('Richard')
names.extend(['Abi', 'Kevin'])
print(names)
An element or a subset of a list can be accessed using element's index or slice of indices.
The same notation applies for strings but at the character level.
# some_list[index]
# some_list[start_index: end_index(not included)]
numbers = [0, 1, 2, 3, 4, 5, 6]
# indices start from 0 in python
print(f'The first element in numbers is {numbers[0]}')
print(f'The third element in numbers is {numbers[2]}')
print(f'Elements from 1st to 5th index are {numbers[1:6]}')
print(f'Elements from start to 5th index are {numbers[:6]}')
print(f'Elements from 4th index to end are {numbers[4:]}')
print(f'Last Element is {numbers[-1]}')
print(f'Last four element are {numbers[-4:]}')
# changing 1st element in the numbers list
numbers[0] = 100
print(numbers)
# changing first 3 numbers
numbers[0: 3] = [100, 200, 300]
print(numbers)
Q: Define a list of four items representing your favorite hobbies/activities.
- Print only the second and third items using list slicing.
# TODO: Write your code here
On the other hand, tuples are immutable, meaning that you cannot change the content of a tuple once it is created.
# Tuples are immutable lists. They are created using () instead of [].
names = tuple()
names = ('Zach', 'Jay')
print(names[0])
# trying to alter the tuple gives an error
names[0] = 'Richard'
# similar to tuples, strings are also immutable
# empty lists and tuples are considered as False
emptyList = []
emptyTuple = ()
print(f"emptyList is {bool(emptyList)} and emptyTuple is {bool(emptyTuple)}")
The split() method can be used to split a string into a list of substrings. By default, it splits the string by spaces, but you can specify a different separator.
# split method
s = "Hello, World!"
words = s.split()
print(words)
# split with a different separator
s = "apple,banana,orange"
fruits = s.split(',')
print(fruits)
You can use the join() method to concatenate a list of strings into a single string.
# join method
words = ['Hello', 'World']
s = ' '.join(words)
print(s)
Q: See what happens when you try to change the content of a tuple.
- Since you cannot change the content of a tuple, you need to create a new tuple with the new content.
- Create a new tuple with 3 elements based on the first three elements of the tuple you defined above.
# TODO: Write your code here
1.5 Dictionary¶
Dictionary is a collection of key-value pairs. It is unordered, changeable and indexed. Dictionaries are written with curly brackets, and have keys and values.
They can be used for example to associate a name with a phone number.
# hash maps in python are called Dictionaries
# dict{key: value}
# Empty dictionary
phonebook = dict()
# contruction dict using sequences of key-value pairs
dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
# Dictionary with one item
phonebook = {'jack': 4098}
# Add another item
phonebook['guido'] = 4127
print(phonebook)
print(phonebook['jack'])
print(phonebook.items())
print(phonebook.keys())
print(phonebook.values())
print('jack' in phonebook)
print('Kevin' in phonebook)
# Delete an item
del phonebook['jack']
print(phonebook)
# An empty dictionary is considered as False
emptyDict = {}
print(f"emptyDict is {bool(emptyDict)}")
Q: Define a dictionary where the keys are the names of the planets and the values are their positions relative to the Sun (like 3 for Earth).
- Print the dictionary.
# TODO: Write your code here
1.6 Sets¶
Sets are unordered collections of unique elements. They are used to store multiple items in a single variable. Sets are written with curly brackets.
# Sets are unordered collection of unique elements
# Sets are mutable
# Empty set
s = set()
# set of integers
s = {1, 2, 3, 4, 5}
print(f"The set is {s}")
# set of mixed data types
s = {1, 2.0, 'three'}
print(f"The set is {s}")
# set of tuples
s = {('a', 'b'), ('c', 'd')}
print(f"The set is {s}")
# set operations
s1 = {1, 2, 3, 4, 5}
s2 = {4, 5, 6, 7, 8}
print(f"s1 is {s1}")
print(f"s2 is {s2}")
# Union
s3 = s1 | s2
print(f"Union of s1 and s2 is {s3}")
# Intersection
s3 = s1 & s2
print(f"Intersection of s1 and s2 is {s3}")
# Difference
s3 = s1 - s2
print(f"Difference of s1 and s2 is {s3}")
# Symmetric Difference
s3 = s1 ^ s2
print(f"Symmetric Difference of s1 and s2 is {s3}")
# Empty set is considered as False
emptySet = set()
print(f"emptySet is {bool(emptySet)}")
Q: Define two sets of strings with some common elements and some unique elements.
Perform the following operations on the sets:
- Union
- Intersection
- Difference
Print the results.
# TODO: Write your code here
2. Flow control statements¶
Flow control statements are used to control the flow of a program. They include if, elif, and else statements, for and while loops, and break and continue statements.
Differently from other programming languages, Python uses indentation to define code blocks. The code block following an if, elif, else, for, or while statement must be indented.
IMPORTANT: The standard indentation is 4 spaces. Always use the same number of spaces for indentation in the same block of code. Avoid using tabs for indentation. Most of the code editors are configured to automatically convert tabs to spaces.
2.1 if... elif... else...¶
The if statement is used to test a condition. If the condition is true, the code block following the if statement is executed. The elif statement is used to test multiple conditions. If the condition is true, the code block following the elif statement is executed. The else statement is used to execute a block of code if the condition is false.
For instance, this is needed if you want to check if a number is positive, negative, or zero and then decide what to do based on the result.
# if expr1:
# code1
# elif expr2:
# code2
# .
# .
# .
# .
# else:
# code_n
# code1 is executed if expr1 is evaluated to true. Else it moves to expr2 and checks for true
# condition and moves to the next if not true.
# Finally if all the excpression's are false, code_n is executed
x = int(input("Please enter an integer: "))
if x < 0:
x = 0
print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')
Q: Prompt the user to input a number and use an if-elif-else statement to check if the number is negative, zero, or positive.
- Print an appropriate message in each case.
# TODO: Write your code here
If statements can be used in conjunction with comparison operators to create more complex conditions.
x = int(input("Please enter an integer: "))
if x > 0 and x < 10:
print(f'x={x} is a positive single digit number')
else:
print(f'x={x} is not a positive single digit number')
2.2 For loops¶
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
In general, loops are used to repeat a block of code multiple times. The for loop iterates over a sequence of elements, such as a list or a tuple. The for loop can also iterate over a string, which is a sequence of characters.
# for loop is used to iter over any iterable object
# iterating over list
for name in ['Steve', 'Jill', 'Venus']:
print(name)
You can iterate over every character in a string.
# iterating over string
for char in "Hellooooo":
print(char)
To iterate over dictionary keys and values, you can use the items() method. The for loop can deal with multiple variables.
# iterating over dict keys
phone_nos = {"Henry": 6091237458,
"James": 1234556789,
"Larry": 5698327549,
"Rocky": 8593876589}
for name, no in phone_nos.items(): # items() returns a list of tuples
print(f"{name}: {no}")
To iterate over a sequence of numbers we use range() function.
The range() function generates a sequence of numbers. It can take one, two, or three parameters. For example, range(5) generates a sequence of numbers from 0 to 4.
for i in range(5):
print(i)
However, if you want to iterate over a sequence of numbers with a different starting point, you can use range(start, stop, step). For example, range(2, 10, 2) generates a sequence of numbers from 2 to 20 with a step of 2:
for i in range(2, 20, 2):
print(i)
You can use the len() function to get the length of a list or a string and use it in the range() function.
# using len of list/tuple in range
names = ['Steve', 'Rock', 'Harry']
for i in range(len(names)):
print(names[i])
Alternatively, if you want to have both the element in a sequences as well as an index, you can use the enumerate() function.
# using len of list/tuple in range
names = ['Steve', 'Rock', 'Harry']
for i in range(len(names)):
print(f"{i}: {names[i]}")
Q: Using a for loop, print the square of each number in range(1, 6).
# TODO: Write your code here
2.3 While Loop¶
With the while loop we can execute a set of statements as long as a condition is true.
# While loop executes as long as the condition remains true.
# while cond1:
# pass
i = 0
while i < 10:
print(i)
i += 1
Forever loop: The below code runs for ever (don't run it)
# while True:
# print('Forever...')
2.4 Break, Continue and Pass statements¶
break statement is used to exit a loop when a condition is met. continue statement is used to skip the current block and return to the for or while statement. pass statement is used as a placeholder for future code.
# break statement breaks out of the the loop
while True:
print('We’re stuck in a loop...')
break # Break out of the while loop
print("not!")
You can skip the current iteration without exiting the loop by using the continue statement.
# continue statement skips a loop
for i in range(5):
continue
print(i)
This is more usefult when you have a condition to skip the current iteration but you don't want to exit the loop. For instance, you can skip the iteration if the number is even.
for i in range(10):
if i % 2 == 0: # Calculate the remainder of i divided by 2 and check if it is 0
continue
print(i)
The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error. It allows you to write minimal code structure without causing issues during execution, which is helpful in situations where code needs to be developed incrementally.
for i in range(10):
pass
Q: Ask the user for an input, print that input, and then print the input in reverse.
- Use a while loop to repeat this process until the user inputs 'quit'.
# TODO: Write your code here
3. References to other resources¶
You can find more info in the Python documentation or in other tutorials.
Run in Google Colab
View on GitHub