Chapter 1: An Introductory Guide to Python#

Data Types, Data Structures, and Coding Conventions#

Author: Dr. Suborna Ahmed

Learning Objectives:#

  • Understand the fundamentals of Python programming language.

  • Understand the importance of proper variable naming, comments, and code organization.

  • Gain knowledge of basic Python data types such as

    • string

    • integer

    • float

    • boolean

    • None

  • Learn about type casting and how to convert between different data types.

  • Understand the characteristics, properties, and common operations associated with basic data types.

  • Work with modules in Python, understand their purpose, and learn how to import and use them in your code.

1. Jumping Into Python#

1.1 Python As a Calculator#

In Python, you can use the language as a basic math calculator to perform arithmetic operations.

Python supports all the common mathematical operators, including addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**). Additionally, Python follows the standard order of operations, also allowing you to perform complex calculations with parentheses to control the order of operations.

Let’s treat Python as a claulating environment. Try the following basic mathematical calculations:

  1. \(5+8\)

  2. \(5\times9\)

  3. \(100\div50\)

  4. \(2^4\)

  5. \(10\times5+69\div20+6^2\)

# Treating Python as a calculator: run this cell

# 1
print("5 + 8 = ", 5+8)

# 2 
print("5 * 9 = ", 5*9)

# 3
print("100 / 5 = ", 100/5)

# 4 
print("2 ** 4 = ", 2**4)

# 5 
print("10 * 5 + 69 / 20 + 6 ** 2 = ", 10*5+69/20+6**2)
5 + 8 =  13
5 * 9 =  45
100 / 5 =  20.0
2 ** 4 =  16
10 * 5 + 69 / 20 + 6 ** 2 =  89.45
# Printing Output using "print()" function
print(5+8)
13
# Give the result a name and print the output
x = 5+8
print(x)
13

Activity 1

In the following coded chunk, write python codes to solve these questions:

  1. Add some math operators and give it a name

  2. Print the result that stored in one variable you written above

  3. Use the print function to print your calculation and result directly, you can add some explanations in the print function with quotation marks (“”).

# Write your code in this chunk

# Add some math operators and give it a name 

# Print the result that stored in one variable you written above

# Use the print function to print your calculation and result directly

See solution for activity 1 at the end.

1.2 Convention to Name a Variable:#

When naming variables in Python, it is essential to follow naming conventions to enhance code readability and maintainability. Variable names should be descriptive, meaningful, and reflect the purpose or content of the variable. The convention is to use lowercase letters, numbers, and underscores. By adhering to naming conventions, you can improve the clarity and understandability of your code.

1.2.1 Variable Names#

A variable is basically a name that represents, or refers to some value, or an object. In python, a variable has a dynamic data type.

Example 1:

# The following line of code represents the result of multiplying the values of two variables, "Band5" and "MaskedArea". 
# "Band5" and "MaskedArea" could be previously defined variables containing numerical values.
Band5 * MaskedArea

Example 2:

# The following line of code represents the sum of the values of two variables, "NumOfStudents" and "Num_of_Profs". 
# The variable names imply that "NumOfStudents" (the number of studnets) and "Num_of_Profs" (the number of professors) are previously defined variables containing numerical values.
NumOfStudents + Num_of_Profs

1.2.2 Creating Variable Names#

Variable names can consist of letters, digits, and underscore (‘_’).

A few suggestions while creating a variable name:

  • Don’t use only numbers as names

  • Don’t use reserved words: and, if, not, elif …

  • Do be descriptive as possible

  • Do use hungarianNotation_or_Underscores. In Hungarian notation name indicates the intention or the kind of variable.

A variable name cannot:

  • begin with a digit

  • contain mathematical operators, e.g., ‘+’, ‘-’ etc

  • have space

  • ~~Parking Lot B~~

  • ~~point 1~~

  • ~~+point~~

  • ~~1point~~

  • point1

A variable name CANNOT begin with a digit.

# Uncomment the following lines. SyntaxErrors are expected when running this cell
#1point = 123

A variable name CANNOT contain mathematical operators.

# Uncomment the following lines. SyntaxErrors are expected when running this cell
#+point = 123

A variable name CANNOT have space.

# Uncomment the following lines. SyntaxErrors are expected when running this cell
#point 1 = 123

Correct naming examples.
NO SyntaxErrors when running this cell.

num = 123 # num for number
num_1 = 123 # variable name with underscore

A name should:

  • Make sense except some local well known variables, such as e (Exception).

  • lower_case_with_underscores or mixedCase

First letter of a class name should be uppercase, e.g., ParkingLot, Building, Route, Grassland.

# more variable names examples

# assign strings to two variables and print the outputs
Name = "Suborna Ahmed"
print(Name)

Course = "GEM530"
print(Course)


# assign a number (float) to a variable
p = 5.5

# "type()"" is a built-in function used to return the data type of the object
# here variable p is a float
type(p)
Suborna Ahmed
GEM530
float

Where is the error coming from?

# Uncomment the following lines and read the SyntaxErrors to understand the error message
#point 2 = 55
#1point = 55

Note

In each Jupyter Notebook cells, if you don’t use the print( ) function to explicitly print the output, only the result of the last line of code will be automatically displayed. Example is showed in the code cell below.

To see the output of multiple lines or specific calculations, make sure to use print( ) to explicitly display the desired results.

# Without print function it only prints the last item
int(2.2)
float(5)
5.0

2. Data Types in Python#

Python supports various data types to represent different kinds of information. Understanding the basic data types is fundamental as they form the basis for data manipulation and analysis in Python.

  • Strings are used to represent textual data and are enclosed in single (‘’) or double (“”) quotes. They can contain letters, numbers, symbols, and spaces.

  • Numbers in Python are represented by integer and floating-point data types. Both integers and floats are used for numerical calculations and operations in Python.

    • Integers are whole numbers without decimals.

    • Floats are numbers with decimals or numbers expressed in scientific notation.

Examples for three basic data types:

  • String: “hello”

  • Integer: 2, 5

  • Floating-Point numbers: 2.5, 1.0

Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed. More information can be found on this site (optional reading).

2.1. Basic Variable Data Types in Python#

Basic Variable Types

Range

Description

Examples

Conversion(Typecast)

Integer

\(-2^{31}\) \(\sim\) \(2^{32}-1\)

decimal,octal,hexadecimal format

20, -20, 010

int()
eg., int(2.0), int(“2”)

Float

Depends on machine arch and compiler of python interpreter

Denoted by a decimal point (.)

0.0, -77.0, 1.6, 2.3e25, 4.3e-2

float()
eg., float(2)

String

Denoted by (‘single quotation marks’) or (“double quotation marks”)

‘test’, “test”

str()
eg., str(2.0)

2.2. Python has Five Standard Built-in Data Types:#

  1. Numbers: int, float

  2. String

  3. List

  4. Tuple

  5. Dictionary

The last three (List, Tuple, Dictionary) are built-in data structures in Python, which will be discussed in week2 material.

2.3 Data Type: Numbers#

  • Editable = mutable

    • Meaning of mutable: a mutable object can be altered after creating it, and it’s the opposite for the immutable object

  • Type can be defined, or assigned at variable creation. 1, -1, 40000000, 3.141579

  • Float, int, long, byte, etc.

    • We only focus on float and int here.

  • Anything numeric!

p = 5 # create a variable p and assign it with an integer
print(type(p)) # print the data type of variable p

p1 = 3.141579 # create a variable p1 and assign it with a float
print(type(p1)) # print the data type of variable p1
<class 'int'>
<class 'float'>

Numbers or Numerical Data Types are Further Classified as:

  1. int (signed int)

    • Singed int is an integer in the range \([-2^{31}, 2^{31}-1]\).

    • Unsigned int is a non-negative integer in the range \([0, 2^{32}-1]\).

    • In Python, we only consider signed integers.

  2. float

More information about complex numbers in python can be found on this site (optional reading).

There is no ‘long integer’ in Python 3 anymore but you can try in PyCharm 2.7.

Conversion Between Integer and Float .#

Conversion codes:

int(x): x converted to integer
This function trims the values after the decimal point and returns only the integer/whole number part.

float(x): x converted to floating point

# convert a float to an integer, only the values before the decimal point will be kept 
int(2.6)
2
# convert an integer to a float
float(2)
2.0

String cannot be convert to number

# uncomment the code below, an Error is expected
#int("s")

2.4. Data Type: Text#

  • Editable = mutable

  • No defined length

  • String: set off with “ and closed with ”

  • Able to be sliced, as cut up sections according to an index of the characters

# Example 1
# create a string variable and print out
p = 'British Columbia'
print(p)

# print out the data Type
print(type(p))
British Columbia
<class 'str'>

Note

Select some letters - Slicing

In Python, slicing on strings allows you to extract substrings or specific portions of a string. When slicing a string, the character at the start index is included in the slice, but the character at the end index is not. It means that string[start:end] extracts characters from start up to, but not including, the character at end.

Python uses zero-based indexing, meaning the first element has index of 0.

# extracts a substring from the string p, starting from index 0 up to, but not including, index 2
# the first two elements (which has index 0 and 1 respectively) are extracted
print(p[0:2])
Br
# Example 2
# create a string variable and print out the type
city_name = 'Vancouver'
print(type(city_name))
<class 'str'>

Number with quotation marks is a string.

# Example 3
# Although 101 is a number, '101' with quotation marks is a string
Section_number = '101'

print(type(Section_number))
<class 'str'>

String Operations:

  1. combining strings with + operator.

  2. repeating the string n times with * operator.

# combining strings use the + operator
city_name = 'Vancouver, '
province = "BC"
city_pv = city_name + province
print(city_pv)

Country = 'Canada'
Country_pv = Country + '_' + province
print(Country_pv)
Vancouver, BC
Canada_BC
# repeating string 5 times by using "repeat times and * operator"
5 * province
'BCBCBCBCBC'

Activity 2

Write Python code in the chunk below to convert the height variable into a number.
Hint: use slicing to extract the int first.

height = "165cm"
# write codes here

See solution for activity 2 at the end.

2.5 Data Type: Boolean#

  • There are two Boolean values: True and False.

Booleans are often used in conditional statements and logical operations (which will be further discussed in logical operators in week2 material) to make decisions based on the truth value of expressions.

x = 5
y = 3
print("x == y: ", x == y) # == sign here means comparison
print("x > y: ", x > y)
print("x < y: ", x < y)
x == y:  False
x > y:  True
x < y:  False

Activity 3

Without coding, determine the boolean value for the following statements.

  • x = 10

  • y = 20

  1. (x == y) and (x < y)

  2. (x < y) or (x > y)

  3. (x >= y) or ((x < 1) and (x > 10))

See solution for activity 3 at the end.

2.6. Data Type: None#

In Python, the special value “None” is used to represent the absence of a value or the lack of a specific data type. It is often used as a placeholder or default value when a variable does not have a value assigned to it.

None

  • Used to delete a reference to a variable

  • Means empty, or no value here

# None: it has no value
road_name = None 
type(road_name)
NoneType

Note

None is a built-in object in Python and behaves as a singleton, meaning there is only one instance of None in memory.

Writing del varName deletes the reference entirely.

  • For the following example, when we assigned float value to the variable p1, we can use the print( ) function to see the value.

  • But after we use the del statement, the object p1 has been deleted from the computer memory.

  • Thus, when we want to print out the value of p1, Python cannot find the variable and it will cause the NameError to remind you that variable p1 is not defined.

p1 = 3.141579
print(p1)
del p1
#print(p1) #uncomment this line and run this cell again to see what happend
3.141579

3. Modules#

Python has many built-in functions that are stored within packages or modules created by other Python users. Before we use these functions / tools, we need to import modules: you can use the import statement in a code cell to bring in the required modules or libraries.

# import package
import package_name

# After importing the package, we can use the built-in function or objects from the package by typing:
package_name.funciton_name

For the following example, you don’t need to understand the exact funciton codes.

# example 1

# import the numpy library
import numpy
# use the function in numpy library to create a multi-dimensional array 
a = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
print(type(a))
[[1 2 3]
 [4 5 6]
 [7 8 9]]
<class 'numpy.ndarray'>

Conventionally, we write:

# import the package
import package as p

# call the function
p.function_name
# example 2

# import the numpy library
import numpy as np
# use the function in numpy library to create a 1-dimensional array a sequence of numbers 0-6(exclusive)
b = np.arange(6)
print(b)
print(type(b))
[0 1 2 3 4 5]
<class 'numpy.ndarray'>

Activity 4

In the following coded chunk, write python codes to solve these questions:

  1. Import a package named datetime by typing import datetime

  2. Create a variable name “current_date_time” and add a code datetime.datetime.now() to get the current date and time.

  3. If you want to know the year then write the variable name then write variable name.year

  4. Print the current year using the variable current_date_time

# write codes here

See solution for activity 4 at the end.

Solutions#

Activity 1#

# Add some math operators and give it a name 
addition = 5 + 2
subtraction = 5 - 2
multiplication = 5 * 2
division = 5 / 2

# Print the result that stored in one variable you written above
print(addition)

# Use the print function to print your calculation and result directly
print("5 times 25 equals: ", 5*25)
7
5 times 25 equals:  125

Activity 2#

height = "165cm"
height_num = height[0:3]
int(height_num)
# or you can convert it to a float number
#float(height_num)
165

Activity 3#

x = 10
y = 20

print("1. (x == y) and (x < y): ", (x == y) and (x < y))

print("2. (x < y) or (x > y): ", (x < y) or (x > y))

print("3. (x >= y) or ((x < 1) and (x > 10)): ", (x >= y) or ((x < 1) and (x > 10)))
1. (x == y) and (x < y):  False
2. (x < y) or (x > y):  True
3. (x >= y) or ((x < 1) and (x > 10)):  False

Activity 4#

# Import a package name datetime 
import datetime

# Create a variable name "current_date_time" to get the current date and time.
current_date_time = datetime.datetime.now()
print(current_date_time)

text1='Date and current time is:'
print(f"{text1}, {current_date_time}")

# Print the current year using the variable
print(current_date_time.year)
2023-08-28 17:55:45.453387
Date and current time is:, 2023-08-28 17:55:45.453387
2023