0% found this document useful (0 votes)
13 views

CPT211 - Lecture Note 2

The document introduces Python programming concepts such as its history, syntax, variables, data types, arrays, and common operations on arrays. Python is presented as a versatile, easy to learn language used widely in applications like web development, data analysis, AI and more. Key Python concepts like indentation, dynamic typing, and emphasis on readability are explained.

Uploaded by

Ella Robinson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CPT211 - Lecture Note 2

The document introduces Python programming concepts such as its history, syntax, variables, data types, arrays, and common operations on arrays. Python is presented as a versatile, easy to learn language used widely in applications like web development, data analysis, AI and more. Key Python concepts like indentation, dynamic typing, and emphasis on readability are explained.

Uploaded by

Ella Robinson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Department of Computer Science

School of Informa4on & Communica4on Technology


Federal University of Technology Minna

CPT 211: Computer Programming I


Lecture Note 2: Introduc4on to Python Programming

1. Revision of Programming Fundamentals

2. Introduc4on to Programming with Python


Python programming language overview
• Python is a high-level, interpreted programming language known for its
simplicity and readability.
• It was created by Guido van Rossum and first released in 1991.
• Python emphasizes code readability with its clean syntax, making it easy for
beginners to learn and understand.
• Why learn Python?
• Python has gained immense popularity due to its versaDlity and wide range of
applicaDons.
• It is used in various fields such as web development, data analysis, scienDfic
compuDng, machine learning, arDficial intelligence, and more.
• Python's extensive libraries and frameworks make it a powerful tool for rapid
development.
• What can Python do?
Python can be used for a variety of tasks, including:
• WriDng scripts to automate repeDDve tasks. For example, a Python script can
be used to rename a batch of files.

-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")

• Dynamically Typed: Python is dynamically typed, meaning you don't need to


explicitly declare variable types. For example:
x=5
name = "Audu"
price = 50.45
• Readability: Python emphasizes code readability, making use of English
keywords and avoiding complex syntax. For example:

if x == 10 and name == "John":


print("Both condiDons are true")

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:

• Numeric types: Integers (int), floaDng-point numbers (float), and complex


numbers (complex). For example:

age = 25 # this is an integer


height = 1.75 # this is a floaDng-point number
price = 100.50 # this is another floaDng-point number
x = 5 + 2j #this is a complex number
y = 10.5 - 4j #this is another complex number
z = 3j #this is another complex number

• 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

• Mapping type: DicDonary ({key: value}). A dicDonary stores a set of key-value


pairs. The keys are used to access the values. For example:
course = {"code": "CPT211", "Dtle": “Computer Programming I”, "unit":3}

The keys in a dicDonary must be unique to avoid ambiguity. In the example


above, course is a dicDonary with keys code, (tle and unit having values
CPT211, Computer Programming I and 3 respecDvely.

• Boolean type: True or False (bool). For example:


is_raining = True
is_sunny = False

• None type: Represents the absence of a value (None). For example:


result = None

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]

• Adding or removing elements: You modify a list by adding or removing elements.


You can use append and remove operaDons to respecDvely add and remove array
elements.
In the sample arrays above:
fruits.append(“pawpaw”) will add “pawpaw” to the end of the list, to give,
fruits = ["apple", "banana", "mango", "orange", "pawpaw"]

fruits.remove(“orange”) will remove “orrange” from the list, to give


fruits = ["apple", "banana", "mango", "pawpaw"]

… to be con4nued.

-4-

You might also like