Basic python programming
Contents
Basic python programming¶
Running
Following the .gif below, click the rocket symbol () to launch this page as an interactive notebook in Google Colab (faster but requiring a Google account) or Binder.
In Google Colab, click Runtime
-> Run all
to run all the cells in the notebook. In Binder, click Cell
-> Run All
to run all the cells in the notebook.
Variable and expressions¶
Creating variables¶
In Python, variables are names to store values.
The
=
symbol assigns the value on the right to the name on the left.The variable is created when a value is assigned to it.
Variable Naming (click to learn more about Python naming convention)
can only contain letters, digits, and underscore _ (typically used to separate words in long variable names)
cannot start with a digit
are case sensitive (age, Age and AGE are three different variables)
Try the following code in the cell below to assign 1
to a variable x
and "hello world"
to a variable string_varible
:
x = 1
string_variable = "Hello world"
Note
Variables must be created before they are used. If a variable doesn’t exist yet, or if the name has been mis-spelled, Python reports an error.
Warning
Some variable names are reserved for special use in Python. For example, print
is a function that prints the value of a variable. If you try to use print
as a variable name, Python will report an error.
Run the following code to see the full list of reserved words in Python:
import keyword
keyword.kwlist
['False',
'None',
'True',
'and',
'as',
'assert',
'async',
'await',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield']
For loop and if statement¶
for
loop is used to iterate over a sequence (e.g. a string) or other iterable objects.
Example
Display the characters in the string "Hello world"
:
for character in string_variable:
print(character)
H
e
l
l
o
w
o
r
l
d
if
statement is used to execute a block of code if a specified condition is true.elif
is used to check another condition if the first condition is false.else
is used to execute a block of code if all conditions are false.
Examples
if
…else
statement
# check an input number is non-negative or negative
num = 3
# Try these two variations as well.
# num = -5
# num = 0
if num >= 0:
print("{} is a non-negative number".format(num))
else:
print("{} is a negative number".format(num))
3 is a non-negative number
if
…elif
…else
statement
# check an input number is positive, negative, or zero
num = 3.14
# Try these two variations as well:
# num = 0
# num = -2.5
if num > 0:
print("{} is a positive number".format(num))
elif num == 0:
print("The input number is zero")
else:
print("{} is a negative number".format(num))
3.14 is a positive number
Built-in functions¶
There are many built-in functions in Python. For example, the print
function has been used in the above section. This section introduces some common built-in functions:
print
displays the value of an expression.Provide values to the function (i.e., the things to print) in parentheses.
print("Value of the string variable is ", string_variable)
print("The first character of the string variable is ", string_variable[0])
print("The first five characters of the string variable are ", string_variable[:5])
print(x, "+ 1 =", x + 1)
Value of the string variable is Hello world
The first character of the string variable is H
The first five characters of the string variable are Hello
1 + 1 = 2
Type
The
type
function returns the type of an expression.
type(string_variable)
str
type(x)
int
Length
The
len
function returns the length of a string, or the number of elements in other type of variables, such as a list and tuple.
print(len(string_variable))
11
Range
The
range
function returns a sequence of numbers.
for i in range(3):
print("loop: ", i)
loop: 0
loop: 1
loop: 2
Variable types¶
Numbers
Integers (e.g.
1
,2
,3
) and floating point numbers (e.g.1.0
,2.5
,3.14159
) are the two main numeric types in Python.
y = 1
z = 3.14
print(type(y))
print(type(z))
<class 'int'>
<class 'float'>
Strings
Strings are sequences of characters.
Strings are created by enclosing characters in single quotes (
'...'
) or double quotes ("..."
).Strings can be concatenated (glued together) with the
+
operator, and repeated with*
.
print(string_variable + " " + "Python is fun!")
Hello world Python is fun!
print(3 * string_variable)
Hello worldHello worldHello world
Booleans
Booleans are either
True
orFalse
.Booleans are often used in
if
statements to control the flow of a program, or used inwhile
orfor
loops to control the number of times a loop is executed.
create_int_variable = True
if create_int_variable:
new_int_variable = 123
print(new_int_variable)
123
None
None
is a special value that represents the absence of a value.None
is the only value of the typeNoneType
.None
is frequently used to represent the absence of a value, as when default arguments are not passed to a function.None
is also frequently returned by functions that don’t explicitly return anything in order to explicitly signal the absence of a return value.None
is a singleton object, there is only oneNone
object and it is unique.None
is immutable, it cannot be changed in any way.None
is comparable to any other object using theis
operator, but it is never equal to any other object using the==
operator.
none_variable = None
print(none_variable is None)
print(type(none_variable))
True
<class 'NoneType'>
Lists
Lists are ordered sequences of values.
Lists are created by enclosing values in square brackets (
[...]
).Lists can contain values of different types.
Lists can be indexed, sliced, and nested.
Lists are mutable and dynamic.
new_list = [1, 2, 3, 4, 5, None]
Using the append()
method can append an element to the end of the list.
new_list.append("Hello world")
print(new_list)
[1, 2, 3, 4, 5, None, 'Hello world']
Dictionaries
Dictionaries are unordered sets of key: value pairs, and created by enclosing pairs in curly braces (
{...}
).Dictionaries can contain values of different types.
Dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.
Dictionaries are mutable and dynamic.
Dictionaries have no concept of order among elements.
Dictionaries are sometimes found in other programming languages as “associative memories”, “associative arrays”, “associative lists”,“hashes”, “hash tables”, or “maps”.
Tuples
Tuples are ordered sequences of values.
Tuples are created by enclosing values in parentheses (
(...)
).Tuples can contain values of different types.
Tuples can be indexed and sliced.
Tuples are immutable and dynamic.
In this course, tuples are usually used to represent the shapes of vectors and matrices.
new_tuple = (3, 4)
type(new_tuple)
tuple
Type conversion
Python can convert values from one type to another.
This is called type conversion, and is sometimes also called type casting.
The syntax for type conversion is to use the type name as a function.
For example,
int("32")
converts the string32
to an integer, andfloat(32)
converts the integer32
to a floating-point number.Type conversion can also be done with the built-in functions
str()
,int()
, andfloat()
.
a = 1
print(a, type(a))
print(float(a), type(float(a)))
print(str(a), type(str(a)))
1 <class 'int'>
1.0 <class 'float'>
1 <class 'str'>
Indexing and slicing¶
Indexing is used to access a single element of a sequence (e.g. a string, a list, or a tuple).
Each position in the string (first, second, etc.) is given a number. This number is called an index or sometimes a subscript.
Indices are numbered from 0.
Use the position’s index in square brackets to get the character at that position.
print(string_variable[0])
H
Index value can be negative, which counts from the right. For example, the index value -1
refers to the last character in the string, -2
refers to the second-last character, and so on. See the following example to get the last element of a list:
print(new_list[-1])
Hello world
Note
In python, the index starts from 0, not 1.
Slicing is used to access a subsequence of a sequence.
A part of a string is called a substring. A substring can be as short as a single character.
An item in a list is called an element. Whenever we treat a string as if it were a list, the string’s elements are its individual characters.
A slice is a part of a string (or, more generally, a part of any list-like thing).
We take a slice with the notation
[start:stop]
, wherestart
is the integer index of the first element we want andstop
is the integer index of the element just after the last element we want.The difference between
stop
andstart
is the slice’s length.Taking a slice does not change the contents of the original string. Instead, taking a slice returns a copy of part of the original string.
# elements beginning to index 5 (not included)
string_variable[:5]
'Hello'
# elements from index 3 to 5 (not included)
string_variable[3:5]
'lo'
# elements from index index 6 to end
string_variable[6:]
'world'
Exercises¶
1. Create a list with values \(22\), \(55\), \(30\), \(126\), \(198\), and \(225\). Write code to only print the numbers which are divisible by \(5\).
# Write your code below to answer the question
Compare your answer with the reference solution below
list_num = [22, 55, 30, 126, 198, 225]
for num in list_num:
if num % 5 == 0:
print(num)
55
30
225
2. Add the values \(33\) and \(65\) to the list and print all the list values.
# Write your code below to answer the question
Compare your answer with the reference solution below
list_num.append(33)
list_num.append(65)
for num in list_num:
print(num)
22
55
30
126
198
225
33
65
3.
a = 13
b = 4.76
c = 'TransparentML'
d = (2,5,9,14)
e = [3,4,2]
Find out each variable types.
# Write your code below to answer the question
Compare your answer with the reference solution below
a = 13
b = 4.76
c = "TransparentML"
d = (2, 5, 9, 14)
e = [3, 4, 2]
print(a, type(a))
print(b, type(b))
print(c, type(c))
print(d, type(d))
print(e, type(e))
13 <class 'int'>
4.76 <class 'float'>
TransparentML <class 'str'>
(2, 5, 9, 14) <class 'tuple'>
[3, 4, 2] <class 'list'>
4. Print the following pattern using loop.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
# Write your code below to answer the question
Compare your answer with the reference solution below
for i in range(6):
for j in range(i):
print(i, end=" ")
print("")
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5