CPT211 - Lecture Note 2
CPT211 - Lecture Note 2
-1-
• Developing web applicaDons and server-side programming. Python
frameworks like Django and Flask are widely used for web development.
• Analyzing and visualizing data using libraries like NumPy, Pandas, and
Matplotlib. For instance, you can analyze sales data and create charts using
Python.
• Building machine learning models with libraries such as TensorFlow and scikit-
learn. Python's simplicity and extensive libraries make it popular in the field
of AI and ML.
• CreaDng GUI (Graphical User Interface) applicaDons using frameworks like
Tkinter or PyQt. You can develop desktop applicaDons with user-friendly
interfaces using Python.
3. Language Syntax
Python uses a clean and straighXorward syntax that is easy to understand.
Key features of Python syntax include:
• Indenta4on: Python uses indentaDon (whitespace at the beginning of a line)
to indicate code blocks, eliminaDng the need for explicit braces or keywords.
For example:
if x > 5:
print("x is greater than 5")
4. Comments
Comments are used to add explanatory notes within the code, which are ignored by
the interpreter.
In Python, comments start with the '#' symbol and conDnue unDl the end of the line.
For example:
#This is a comment
x = 5 #Assigning a value to x
5. Variables
Variables are used to store values that can be accessed and manipulated during
program execuDon. In Python, variables are created by assigning a value to a name.
For example:
x=5
name = "John"
-2-
6. Data Types
Python supports several built-in data types:
• Sequence types: Strings (str), lists ([]), and tuples (()). For example:
name = "Garba Chinedu” # this is a string
fruits = ["apple", "banana", "orange"] # this is a list
coordinates = (10, 20) # this is a tuple
7. Arrays
Arrays, also known as lists in Python, are used to store mulDple values in a single
variable. Arrays can contain elements of different data types and can be modified
(mutable). For example:
fruits = ["apple", "banana", "mango", "orange"] # an array of string data types
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # an array of integer data types
mixed_list = [10, "John", True, 3.14] # an array of mixed data types
-3-
In strongly typed programming languages such as Java, C++, etc. where arrays can only
store elements of the same type. i.e., if an array is declared as an array of integers, it
cannot store float or string data. This is, however, possible in Python because it is
dynamically typed. i.e., you do not need to declare the type of a variable; it assumes
the type of whatever value is assigned to it at any Dme. Thus, an array can store
elements of different types in Python as illustrated in the example above. You will
noDce that while array fruits is an array of string values and numbers is an array of
integer values, mixed_list is an array of integer, string, boolean and float data types.
List opera4ons:
Now let us discuss the operaDons by which you can read, write into, and manipulate
arrays in Python.
• Indexing: You can accessing individual elements in a list using their index. An index
refers to a posiDon in the list. Note that Indexing starts from 0 in Python like many
other contemporary programming languages. Considering array fruits in the
example above,
print(fruits[0]) will output “apple”
print(fruits[1]) will output “banana”
print(fruits[2]) will output “mango”
print(fruits[3]) will output “orange”
similarly, print(mixed_list[2]) will output True while print(numbers[2]) will output
3
• Slicing: You can use slicing to extracDng a porDon of a list. Slicing uses the syntax
[start:end], where start is the inclusive starDng index and end is the exclusive
ending index. For example, considering the three arrays in the example above,
fruits[1:3] will produce ["banana", "mango"]
numbers[3:8] will produce [4, 5, 6, 7, 8]
mixed_list[1:3] will produce ["John", True]
… to be con4nued.
-4-