FEDERAL UNIVERSITY DUTSINMA
DEPARTMENT OF COMPUTER SCIENCE AND AI
CMP 221
Introduction To Programming 1
python Programming
12/7/2023 M.A Mashi & M.M Yakubu
Course Content
 Overview of programming language
 History and features of python
 why python
 Variables
 Variable Declaration
 Data types
 Errors in python
 Initialization of a variable
 Expressions
 Operators
 Conditional Statement
 Iterative statement
 Functions
 Arrays
12/7/2023 M.A Mashi & M.M Yakubu
Recommended Textbooks
• Eric Matthes (2019). Python Crash Course, 2nd
Edition: A Hands – On, Project – Based
Introduction Programming.
• Al Sweigart (2019). Automate the Boring Stuff
with Python, 2nd Edition: Practical Programming
for Total Beginners
• Mart Lutz (2013). Learning Python, 5th Edition
• Paul Barry (2016). Head First Python: A Brain –
Friendly Guide, 2nd Edition.
• Tutorials Point, Simply Easy Learning (2016).
Python 3.
12/7/2023 M.A Mashi & M.M Yakubu
General Overview
 The functions of a computer system are controlled by computer
programs
 A computer program is a clear, step-by-step, finite set of instructions
 A computer program must be clear so that only one meaning can be
derived from it,
 A computer program is written in a computer language called a
programming language
12/7/2023 M.A Mashi & M.M Yakubu
Programming Languages
There are three categories of programming
languages:
1. Machine languages.
2. Assembly languages.
3. High-level languages.
 Machine languages and assembly languages are also called low-level
languages
12/7/2023 M.A Mashi & M.M Yakubu
Machine Language
 A Machine language program consists of a sequence of
zeros and ones.
 Each kind of CPU has its own machine language.
 Advantages
 Fast and efficient
 Machine friendly
 No translation required
 Disadvantages
 Not portable
 Not programmer friendly
12/7/2023 M.A Mashi & M.M Yakubu
Assembly language
 Assembly language programs use mnemonics to represent machine
instructions
 Each statement in assembly language corresponds to one statement in
machine language.
 Assembly language programs have the same advantages and
disadvantages as machine language programs.
12/7/2023 M.A Mashi & M.M Yakubu
High Level Language
 A high-level language (HLL) has two primary components
 (1) a set of built-in language primitives and grammatical rules
 (2) a translator
 A HLL language program consists of English-like statements that are governed by a
strict syntax.
 Advantages
 Portable or machine independent
 Programmer-friendly
 Disadvantages
 Not as efficient as low-level languages
 Need to be translated
 Examples : python, C, C++, Java, FORTRAN, Visual Basic, and Delphi
12/7/2023 M.A Mashi & M.M Yakubu
Features of python
• Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows
a student to pick up the language quickly.
• Easy-to-read: Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain: Python's source code is fairly easy-to-maintain.
• A broad standard library: Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
• Interactive Mode: Python has support for an interactive mode, which allows interactive testing and
debugging of snippets of code.
• Portable: Python can run on a wide variety of hardware platforms and has the same interface on all
platforms.
• Extendable: You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
• Databases: Python provides interfaces to all major commercial databases.
• GUI Programming: Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window
system of UNIX.
• Scalable: Python provides a better structure and support for large programs than shell scripting.
12/7/2023 M.A Mashi & M.M Yakubu
What python can do?
• Python can be used on a server to create web
applications (Server – Side).
• Python can be used alongside software to
create workflows.
• Python can connect to database systems. It can
also read and modify files.
• Python can be used to handle big data and
perform complex mathematics.
• Python can be used for rapid prototyping, or
for production-ready software development.
• Python can be used for Software development
and System scripting
12/7/2023 M.A Mashi & M.M Yakubu
Why python
• Python works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc).
• Python has a simple syntax similar to the English
language.
• Python has syntax that allows developers to write
programs with fewer lines than some other
programming languages.
• Python runs on an interpreter system, meaning that
code can be executed as soon as it is written. This means
that prototyping can be very quick.
• Python can be treated in a procedural way, an object-
oriented way or a functional way.
12/7/2023 M.A Mashi & M.M Yakubu
THREE DIFFERENT WAYS TO RUN
PYTHON
1. Interactive Interpreter
• You can start Python from UNIX, DOS, or any other system
that provides you a command-line interpreter or shell
window.
2. Script from the Command-line
A Python script can be executed at the command line by
invoking the interpreter on your application, as shown in the
following example
12/7/2023 M.A Mashi & M.M Yakubu
Cont…………..
$python script.py # Unix/Linux
Or
python%script.py # Unix/Linux
or
C:>python script.py # Windows/DOS
(3) Integrated Development Environment
You can run Python from a Graphical User Interface (GUI) environment as well, if you
have a GUI application on your system that supports Python.
• UNIX: IDLE is the very first Unix IDE for Python.
• Windows: Python Win is the first Windows interface for Python and is an
IDE with a GUI.
• Macintosh: The Macintosh version of Python along with the IDLE IDE is
available from the main website, downloadable as either Mac Binary or BinHex'd
files.
12/7/2023 M.A Mashi & M.M Yakubu
Comments
 Comments can be used to explain Python code.
 Comments can be used to make the code more readable.
 Comments can be used to prevent execution when testing code.
Comments starts with a #, and Python will ignore them:
Types of Comments.
1. Single Line comment
#This is a code to print hello world.
print("Hello, World!“)
2. Multiline Comment
X=10 # x hold the value of 10
print (“Welcome to cmp 221”) # display welcome to cmp 221 on the monitor
12/7/2023 M.A Mashi & M.M Yakubu
VARIABLE
• Variables are nothing but reserved memory locations to store
values. This means that when you create a variable you reserve
some space in memory.
• A Python variable is a reserved memory location to store values. In
other words, a variable in a python program gives data to the
computer for processing.
• Python Variable Types
• Every value in Python has a datatype. Different data types in Python
are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python
can be declared by any name or even alphabets like a, aa, abc, etc
• Based on the data type of a variable, the interpreter allocates
memory and decides what can be stored in the reserved memory. .
Therefore, by assigning different data types to variables, you can
store integers, decimals or characters in these variables.
12/7/2023 M.A Mashi & M.M Yakubu
Rules for Naming a Variables
• A variable name must start with a letter or
the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-
numeric characters and underscores (A-z, 0-
9, and _ )
• Variable names are case-sensitive (age, Age
and AGE are three different variables)
12/7/2023 M.A Mashi & M.M Yakubu
Data Types
• Variables can store data of different types,
and different types can do different things.
• Python has the following data types built-in
by default, in these categories:
Text Type: str
Numeric
Types:
int, float, complex
Sequence
Types:
list, tuple, range
Mapping
Type:
dict
Set Types: set, frozenset
Boolean
Type:
bool
Binary bytes, bytearray, memoryview
12/7/2023 M.A Mashi & M.M Yakubu
DATA TYPE
int long float complex
10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAEC
BFBAEl
32.3+e18 .876j
-0490 535633629843L -90. -.6545+0J
-0x260 -052318172735L -32.54e100 3e+26J
0x69 -4721885298529L 70.2-E12 4.53e-7
12/7/2023 M.A Mashi & M.M Yakubu
Errors in python
1. Compile Time Error
2. Run Time Error
3. Logical Error.
12/7/2023 M.A Mashi & M.M Yakubu
Discuss in the class with relevant
examples
Getting data types
You can get the data type
of any object by using the
type() function.
integer
Number =5
Print(type(number)) output
<class 'int'>
float
x = 20.5
#display x:
print(x)
#display the data type of x:
print(type(x)) output
x= 20.5
<class
‘flaot’>
String
x = "Hello World"
#display x:
print(x)
#displa
y the data type of x:
print(type(x)) Output
Hello World
<class 'str'>
List
x = ["apple", "banana", "cherry"]
#display x:
print(x)
#display the data type of x:
print(type(x))
Output
['apple',
'banana', 'cherry']
<class
'list'>
12/7/2023 M.A Mashi & M.M Yakubu
Complex
x = 1j
#display x:
print(x)
#display the data type of x:
print(type(x))
output
lj
<class
'complex'>
Dictionary
x = {"name" : "John", "age" :
36}
#display x:
print(x)
#display the data type of x:
print(type(x))
output
{'name': 'John', 'age':
36}
<class 'dict'>
12/7/2023 M.A Mashi & M.M Yakubu
Data type
Boolean
x = True
#display x:
print(x)
#display the data type of x:
print(type(x))
Output
True
<class 'bool'>
Complex
12/7/2023 M.A Mashi & M.M Yakubu
Class Activities

Lecture1.pptx

  • 1.
    FEDERAL UNIVERSITY DUTSINMA DEPARTMENTOF COMPUTER SCIENCE AND AI CMP 221 Introduction To Programming 1 python Programming 12/7/2023 M.A Mashi & M.M Yakubu
  • 2.
    Course Content  Overviewof programming language  History and features of python  why python  Variables  Variable Declaration  Data types  Errors in python  Initialization of a variable  Expressions  Operators  Conditional Statement  Iterative statement  Functions  Arrays 12/7/2023 M.A Mashi & M.M Yakubu
  • 3.
    Recommended Textbooks • EricMatthes (2019). Python Crash Course, 2nd Edition: A Hands – On, Project – Based Introduction Programming. • Al Sweigart (2019). Automate the Boring Stuff with Python, 2nd Edition: Practical Programming for Total Beginners • Mart Lutz (2013). Learning Python, 5th Edition • Paul Barry (2016). Head First Python: A Brain – Friendly Guide, 2nd Edition. • Tutorials Point, Simply Easy Learning (2016). Python 3. 12/7/2023 M.A Mashi & M.M Yakubu
  • 4.
    General Overview  Thefunctions of a computer system are controlled by computer programs  A computer program is a clear, step-by-step, finite set of instructions  A computer program must be clear so that only one meaning can be derived from it,  A computer program is written in a computer language called a programming language 12/7/2023 M.A Mashi & M.M Yakubu
  • 5.
    Programming Languages There arethree categories of programming languages: 1. Machine languages. 2. Assembly languages. 3. High-level languages.  Machine languages and assembly languages are also called low-level languages 12/7/2023 M.A Mashi & M.M Yakubu
  • 6.
    Machine Language  AMachine language program consists of a sequence of zeros and ones.  Each kind of CPU has its own machine language.  Advantages  Fast and efficient  Machine friendly  No translation required  Disadvantages  Not portable  Not programmer friendly 12/7/2023 M.A Mashi & M.M Yakubu
  • 7.
    Assembly language  Assemblylanguage programs use mnemonics to represent machine instructions  Each statement in assembly language corresponds to one statement in machine language.  Assembly language programs have the same advantages and disadvantages as machine language programs. 12/7/2023 M.A Mashi & M.M Yakubu
  • 8.
    High Level Language A high-level language (HLL) has two primary components  (1) a set of built-in language primitives and grammatical rules  (2) a translator  A HLL language program consists of English-like statements that are governed by a strict syntax.  Advantages  Portable or machine independent  Programmer-friendly  Disadvantages  Not as efficient as low-level languages  Need to be translated  Examples : python, C, C++, Java, FORTRAN, Visual Basic, and Delphi 12/7/2023 M.A Mashi & M.M Yakubu
  • 9.
    Features of python •Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows a student to pick up the language quickly. • Easy-to-read: Python code is more clearly defined and visible to the eyes. • Easy-to-maintain: Python's source code is fairly easy-to-maintain. • A broad standard library: Python's bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh. • Interactive Mode: Python has support for an interactive mode, which allows interactive testing and debugging of snippets of code. • Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms. • Extendable: You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient. • Databases: Python provides interfaces to all major commercial databases. • GUI Programming: Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of UNIX. • Scalable: Python provides a better structure and support for large programs than shell scripting. 12/7/2023 M.A Mashi & M.M Yakubu
  • 10.
    What python cando? • Python can be used on a server to create web applications (Server – Side). • Python can be used alongside software to create workflows. • Python can connect to database systems. It can also read and modify files. • Python can be used to handle big data and perform complex mathematics. • Python can be used for rapid prototyping, or for production-ready software development. • Python can be used for Software development and System scripting 12/7/2023 M.A Mashi & M.M Yakubu
  • 11.
    Why python • Pythonworks on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). • Python has a simple syntax similar to the English language. • Python has syntax that allows developers to write programs with fewer lines than some other programming languages. • Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick. • Python can be treated in a procedural way, an object- oriented way or a functional way. 12/7/2023 M.A Mashi & M.M Yakubu
  • 12.
    THREE DIFFERENT WAYSTO RUN PYTHON 1. Interactive Interpreter • You can start Python from UNIX, DOS, or any other system that provides you a command-line interpreter or shell window. 2. Script from the Command-line A Python script can be executed at the command line by invoking the interpreter on your application, as shown in the following example 12/7/2023 M.A Mashi & M.M Yakubu
  • 13.
    Cont………….. $python script.py #Unix/Linux Or python%script.py # Unix/Linux or C:>python script.py # Windows/DOS (3) Integrated Development Environment You can run Python from a Graphical User Interface (GUI) environment as well, if you have a GUI application on your system that supports Python. • UNIX: IDLE is the very first Unix IDE for Python. • Windows: Python Win is the first Windows interface for Python and is an IDE with a GUI. • Macintosh: The Macintosh version of Python along with the IDLE IDE is available from the main website, downloadable as either Mac Binary or BinHex'd files. 12/7/2023 M.A Mashi & M.M Yakubu
  • 14.
    Comments  Comments canbe used to explain Python code.  Comments can be used to make the code more readable.  Comments can be used to prevent execution when testing code. Comments starts with a #, and Python will ignore them: Types of Comments. 1. Single Line comment #This is a code to print hello world. print("Hello, World!“) 2. Multiline Comment X=10 # x hold the value of 10 print (“Welcome to cmp 221”) # display welcome to cmp 221 on the monitor 12/7/2023 M.A Mashi & M.M Yakubu
  • 15.
    VARIABLE • Variables arenothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. • A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing. • Python Variable Types • Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc • Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. . Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables. 12/7/2023 M.A Mashi & M.M Yakubu
  • 16.
    Rules for Naminga Variables • A variable name must start with a letter or the underscore character • A variable name cannot start with a number • A variable name can only contain alpha- numeric characters and underscores (A-z, 0- 9, and _ ) • Variable names are case-sensitive (age, Age and AGE are three different variables) 12/7/2023 M.A Mashi & M.M Yakubu
  • 17.
    Data Types • Variablescan store data of different types, and different types can do different things. • Python has the following data types built-in by default, in these categories: Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary bytes, bytearray, memoryview 12/7/2023 M.A Mashi & M.M Yakubu
  • 18.
    DATA TYPE int longfloat complex 10 51924361L 0.0 3.14j 100 -0x19323L 15.20 45.j -786 0122L -21.9 9.322e-36j 080 0xDEFABCECBDAEC BFBAEl 32.3+e18 .876j -0490 535633629843L -90. -.6545+0J -0x260 -052318172735L -32.54e100 3e+26J 0x69 -4721885298529L 70.2-E12 4.53e-7 12/7/2023 M.A Mashi & M.M Yakubu
  • 19.
    Errors in python 1.Compile Time Error 2. Run Time Error 3. Logical Error. 12/7/2023 M.A Mashi & M.M Yakubu Discuss in the class with relevant examples
  • 20.
    Getting data types Youcan get the data type of any object by using the type() function. integer Number =5 Print(type(number)) output <class 'int'> float x = 20.5 #display x: print(x) #display the data type of x: print(type(x)) output x= 20.5 <class ‘flaot’> String x = "Hello World" #display x: print(x) #displa y the data type of x: print(type(x)) Output Hello World <class 'str'> List x = ["apple", "banana", "cherry"] #display x: print(x) #display the data type of x: print(type(x)) Output ['apple', 'banana', 'cherry'] <class 'list'> 12/7/2023 M.A Mashi & M.M Yakubu
  • 21.
    Complex x = 1j #displayx: print(x) #display the data type of x: print(type(x)) output lj <class 'complex'> Dictionary x = {"name" : "John", "age" : 36} #display x: print(x) #display the data type of x: print(type(x)) output {'name': 'John', 'age': 36} <class 'dict'> 12/7/2023 M.A Mashi & M.M Yakubu
  • 22.
    Data type Boolean x =True #display x: print(x) #display the data type of x: print(type(x)) Output True <class 'bool'> Complex 12/7/2023 M.A Mashi & M.M Yakubu
  • 23.