0% found this document useful (0 votes)
17 views123 pages

Python Programming Basics Guide

The document provides an introduction to Python programming, covering its history, syntax, and key concepts such as variables, operators, user input, algorithms, flowcharts, and control flow. It explains how to print output, declare variables, use different types of operators, and handle user input effectively. Additionally, it discusses the importance of algorithms and flowcharts in programming, along with examples of pseudocode and control structures.

Uploaded by

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

Python Programming Basics Guide

The document provides an introduction to Python programming, covering its history, syntax, and key concepts such as variables, operators, user input, algorithms, flowcharts, and control flow. It explains how to print output, declare variables, use different types of operators, and handle user input effectively. Additionally, it discusses the importance of algorithms and flowcharts in programming, along with examples of pseudocode and control structures.

Uploaded by

Gareth Matina
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

PYTHON

PROGRAMMING

TAURAI MAMIRE

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
WHAT IS PYTHON?

PRINTING ON SCREEN

STRINGS

VARIABLES

OPERATORS

USER INPUT

ALGORITHMS, FLOWCHARTS
Outline
AND PSEUDOCODE

CONTROL FLOW

LISTS,SETS,TUPLES AND
DICTIONARY
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
Introduction
Why Python?
Python works on different
What is Python? platforms (Windows, Mac, Linux,
Python is a popular Raspberry Pi, etc).
programming language. It was Python has a simple syntax similar
created by Guido van Rossum, to the English language.
and released in 1991. Python has syntax that allows
It is used for: developers to write programs with
 web development (server- fewer lines than some other
programming languages.
side),
Python can be treated in a
 software development,
procedural way, an object-
 mathematics, oriented way or a functional way.
 Statistics etc
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
HOW TO PRINT.

Python Print() Statement:


The print command in Python prints strings or objects
which are converted to a string while printing on a
screen.

print(“Hello World”)

More printing examples

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
STRINGS

What is a string?
A string is a series of characters.
String make up words, phrases and sentences.

How to print strings


print(“My name is John Doe”)

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
PYTHON COMMENTS

Comments in Python Programming


language are used to provide information
about lines of code. It is widely used for
documenting code. There are 2 types of
comments in the Python language:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
COMMENTS

#This is a comment
SINGLE LINE print("Hello, World!")

"""
This is a comment
written in
MULTI-LINE more than just one line
""“
print("Hello, World!")

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
ESCAPE SEQUENCES

Escape characters can be classified as


non-printable characters when
backslash(\) precedes them. The print
statements do not print escape
characters.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
ESCAPE SEQUENCES

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
VARIABLES

What is a Variable in PROGRAMMING?


A 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.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
VARIABLE DATA TYPES

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

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
DECLARING VARIABLES

How to Declare and use a Variable

a=100
print (a)

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
OPERATORS IN PYTHON

An operator is a symbol that tells the


compiler to perform specific
mathematical or logical functions.
Python language is rich in built-in
operators and provides the following
types of operators :
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
OPERATORS
The following types are covered during this course:

Arithmetic operators Logical operators


01 02

Assignment operators Identity operators


03 04

Membership
Comparison operators
05 06 operators

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
ASSIGNMENT OPERATOR
 Assignment operators are used to assign values to
variables:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
ASSIGNMENT OPERATOR
 Assignment operators are used to assign values to
variables:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
ARITHEMATIC OPERATOR
 Arithmetic operators are used with numeric values to
perform common mathematical operations:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
COMPARISON OPERATOR
 Comparison operators are used to compare two
values:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
LOGICAL OPERATOR
 Logical operators are used to combine conditional
statements:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
IDENTITY OPERATOR
 Identity operators are used to compare the objects,
not if they are equal, but if they are actually the same
object, with the same memory location:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
MEMBERSHIP OPERATOR
 Membership operators are used to test if a sequence
is presented in an object:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
USER INPUT

To receive information through the keyboard,


Python uses the input() function. This function
has an optional parameter, commonly known as
prompt, which is a string that will be printed on
the screen whenever the function is called.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
USER INPUT

When the input()function is called, the


program flow stops until the user enters
the input via the command line. To
actually enter the data, the user needs to
press the ENTER key after inputting their
string.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
USER INPUT

Now that we understand the basic theory


behind the input() function, let's take a
look at how it actually works in Python:

txt = input("Type something to test this out: ")


print(f"Is this what you just said? {txt}")
print("Is this what you just said? “,text)
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
USER INPUT

The input() function, by default, will convert all


the information it receives into a string. The
previous example we showed demonstrates this
behavior.
Numbers, on the other hand, need to be
explicitly handled as such since they come in as
strings originally. The following example
demonstrates how numeric type information is
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
USER INPUT

# An input is requested and stored in a variable


test_text = input ("Enter a number: ")

# Converts the string into an integer. If you need


# to convert the user input into the decimal format,
# the float() function is used instead of int()
test_number = int(test_text)
# Prints in the console the variable as requested
print ("The number you entered is: ", test_number)
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
USER INPUT

The more common approach is to perform both


reading input and converting it into an integer in
one line:

test_number = int(input("Enter a number: "))

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
ALGORITHMS,
FLOWCHARTS
AND PSEUDO
CODE
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
ALGORITHMS

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
ALGORITHMS
An algorithm is a representation of a
solution to a problem. If a problem
can be defined as a difference
between a desired situation and the
current situation in which one is, then
a problem solution is a procedure, or
method, for transforming the current
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
ALGORITHMS
Definition:
An algorithm is procedure consisting of a finite
set of unambiguous rules (instructions) which
specify a finite sequence of operations that
provides the solution to a problem, or to a
specific class of problems for any allowable set
of input quantities (if there are inputs).
In other word, an algorithm is a step-by-step
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
ALGORITHMS
• Alternatively, we can define an algorithm as a
set or list of instructions for carrying out some
process step by step. A recipe in a cookbook
is an excellent example of an algorithm.
• The recipe includes the requirements for the
cooking or ingredients and the method of
cooking them until you end up with a nice
cooked dish. Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
ALGORITHMS
• In the problem-solving phase of computer
programming, you will be designing
algorithms.
• This means that you will have to be conscious
of the strategies you use to solve problems in
order to apply them to programming problems.
• These algorithms can be designed though the
use of flowcharts or pseudocode.
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
ALGORITHMS
Here is an example of an algorithm for a
program that adds two numbers:
• STEP 1 : START
• STEP 2 : ACCEPT FIRST NUMBER
• STEP 3 : ACCEPT SECOND NUMBER
• STEP 4 : ADD THESE TWO NUMBERS
• STEP 5 : DISPLAY RESULT
• STEP 6 : STOP

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
FLOWCHART
• Flowcharting is a tool developed in the
computer industry, for showing the steps
involved in a process.
• A flowchart is a diagram made up of boxes,
diamonds and other shapes, connected by
arrows - each shape represents a step in the
process, and the arrows show the order in which
they occur. Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
• In computing, there are dozens of different
FLOWCHART
symbols used in flowcharting (there are
even national and international
flowcharting symbol standards).
• In business process analysis, a couple of
symbols are sufficient.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
• Here are the basic flowchart symbols:
FLOWCHART

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
• Here is an example of a flow chart for a
FLOWCHART
program that adds two numbers:

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
star
t

Input num1

Input num2

Add num1 and num2

Store result

stop
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
PSEUDO
• Pseudocode is one of the tools that can be used to write a

program. CODE
preliminary plan that can be developed into a computer

• Pseudocode is a generic way of describing an algorithm


without use of any specific programming language syntax.
• It is, as the name suggests, pseudo/false code —it cannot be
executed on a real computer, but it models and resembles
real programming code, and is written at roughly the same
level of detail.
• Pseudocode, by nature, exists in various forms, although most
borrow syntax from popular programming languages
Free SlideSalad PowerPoint Template
(likeAllC,
Copyright (C) [Link] rights
reserved.
PSEUDO
• considering our example of adding two numbers that we did

CODE
an algorithm and a flow chart for, here is how the pseudo
code will look like:
• BEGIN
• NUMBER s1, s2, sum
• OUTPUT("Input number1:")
• INPUT s1
• OUTPUT("Input number2:")
• INPUT s2
• sum=s1+s2
• OUTPUT sum
• END

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
CONTROL FLOW

A program’s control flow is the order in which the


program’s code executes.
The control flow of a Python program is regulated by
conditional statements, loops, and function calls.
Python has three types of control structures:
•Sequential - default mode.
•Selection - used for decisions and branching.
•Repetition - used for looping, i.e., repeating a piece of
code multiple times.
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
CONTROL FLOW

SEQUENTIAL SELECTION
01 02

REPETITION
03

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
SEQUENTIAL
 Sequential statements are a set of statements
whose execution process happens in a sequence. The
problem with sequential statements is that if the logic
has broken in any one of the lines, then the complete
##source
This iscode execution statement
a Sequential will break.

a=20
b=10
c=a-b
print("Subtraction is : ",c)

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
SELECTION/DECISION

In Python, the selection statements are also


known as Decision control
statements or branching statements.
The selection statement allows a program to
test several conditions and execute
instructions based on which condition is true.
Some Decision Control Statements are:
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
SELECTION/DECISION MAKING

IF IF-ELSE
01 02

IF –ELIF-ELSE
03

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
If statement FLOW CHART

Simple if: If
statements are control
flow statements that
help us to run a
particular code, but
only when a certain
condition is met or
satisfied. A simple if
has only one condition
to check.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
if syntax EXAMPLE

SYNTAX

if condition:
#statements age = 15
if age < 18:
print(“VOTING NOT
ALLOWED")

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
if-else statement FLOW CHART

The if-else
statement
evaluates the
condition and will
execute the body of
if if the test
condition is True,
but if the condition
is False, then the
body of else is Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
executed. reserved.
if syntax EXAMPLE

SYNTAX

if condition: age = 15
if age < 18:
#statements print(“ALLOWED VOTE")
else: else:
#statements print(“NOT ALLOWED TO
VOTE")

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
if-elif-else FLOW CHART
statement
if -elif-else
statement
executes one
condition
among several
conditions.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
If-elif-else EXAMPLE
syntax
SYNTAX
age = 15
if condition: if age < 18:
#statements print(“ALLOWED VOTE")
elif condition: elif age>=18:
#statements print(“NOT ALLOWED TO
VOTE")
else:
else:
#statements print(“INVALID”)

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
REPETITION/LOOPING

Repetition or looping refers to the specified sequence


in which a pattern or a block of code is repeated until
the condition becomes no longer true. As long as the
condition remains to be true, the loop is executed. It is
terminated when the condition is false or no longer
satisfied.

In Python, we generally have two loops/repetitive


statements:
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
SELECTION/DECISION MAKING

01 while 02 for

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
WHILE LOOP

while loop: In Python, while loops are used to


execute a block of statements repeatedly until
a given condition is satisfied. Then, the
expression is checked again and, if it is still
true, the body is executed again. This
continues until the expression becomes false.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
WHILE LOOP FLOW CHART SYNTAX and example

Syntax:
while condition:
#statements
increment/decrement

Example:

i=1
while i < 6:
print(i)
i += 1 Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
for LOOP

for loop: A for loop is used to


iterate over a sequence that is
either a list, tuple, dictionary, or a
set. We can execute a set of
statements once for each item in a
list, tuple, or dictionary.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
FOR LOOP FLOW CHART SYNTAX and example

Syntax:
for condition:
#statements

Example:

for j in
range(0,10):
print(j, end = "
")
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
break

The break is a keyword in python which is


used to bring the program control out of the
loop.
We can say that break is used to abort the
current execution of the program and the
control goes to the next line after the loop.
The break is commonly used in the cases
where we need to break the loop for a given
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
break

for i in range(10):
if i==5:
break
print(i)

Output: 1 2 3 4 5
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
continue

The continue statement is used to


bring the program control to the
beginning of the loop.
The continue statement skips some lines
of code inside the loop and continues with
the next iteration.
It is mainly used for a condition so that we
can skip some code for a particular
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
continue

# Python code to show example of continue statement

# looping from 10 to 20
for iterator in range(10, 21):

# If iterator is equals to 15, loop will continue to the nex


t iteration
if iterator == 15:
continue
# otherwise printing the value of iterator
Copyright (C) [Link] All rights
print( iterator )
Free SlideSalad PowerPoint Template
reserved.
LISTS

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
LISTS

WHAT IS A LIST?
A list can be defined as a collection of
values or items of different types.
The items in the list are separated
with the comma (,) and enclosed with
the square brackets [].
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
LISTS

names = ["John", Taurai, “Monica"]

marks = [91, 82, 73, 64, 65, 76]

countries= [“Zimbabwe",”China”, “Russia”, ”Ukraine"]

mixed= ["John", 102, "USA"]

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
LISTS-CHARACTERISTICS

 The list has the following


characteristics:
 The lists are ordered.
 The element of the list can be accessed
by index.
 The lists are the mutable type.
 The lists are mutable types.
 A list can store the number of various
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
LISTS-Accessing values
 To access values in lists, use the square brackets for slicing along with
the index or indices to obtain value available at that index. For example

list1 = ['physics', 'chemistry', 1997, 2000];


list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

Output list1[0]: physics


list2[1:5]: [2, 3, 4, 5]
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
LISTS-Updating lists
 You can update single or multiple elements of lists by giving the
slice on the left-hand side of the assignment operator, and you
can add to elements in a list with the append() method. For
example −
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
print list[2]
Value available at index 2 : 1997
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
LISTS-Deleting
 To remove a list element, you can use either the del statement if
you know exactly which element(s) you are deleting or the
remove() method if you do not know. For example −
list1 = ['physics', 'chemistry', 1997, 2000];
print list1
del list1[2];
print "After deleting value at index 2 : "
print list1
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
LISTS-BASIC OPERATIONS
Lists respond to the + and * operators much like strings; they mean
concatenation and repetition here too, except that the result is a new list,
not a string.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
LISTS-indexing,slicing and matrices
Because lists are sequences, indexing and slicing work the same way for
lists as they do for strings.
Assuming following input −

L=[“spam”,”Spam”,”SPAM”]

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
LISTS-iterating or looping through

You can print all the elements of a list as shown below:

names=[“John”,”Chiyedza”,”Hazvinei”]

for x in names:
print(x)

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
LISTS-iterating or looping through

thislist = ["apple", "banana", "cherry"]


for i in range(len(thislist)):
print(thislist[i])

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
TUPLES

 A tuple is a collection of objects which


ordered and immutable.
 Tuples are sequences, just like lists.
 The differences between tuples and
lists are, the tuples cannot be changed
unlike lists and tuples use parentheses,
whereas lists use square brackets.
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
TUPLES

Creating a tuple is as simple as


putting different comma-separated
values. Optionally you can put these
comma-separated values between
parentheses also. For example −
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = 1, 2, 3, 4, 5, 6, 7 ;
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
TUPLES-Accessing values
To access values in tuple, use the square brackets for slicing along with the
index or indices to obtain value available at that index.
For example −
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0];
print "tup2[1:5]: ", tup2[1:5];
Output
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
TUPLES-updating

 Tuples are immutable which means


you cannot update or change the
values of tuple elements.
 You are able to take portions of
existing tuples to create new
tuples as the following example
demonstrates.
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
TUPLES-updating

tup1 = (12, 34.56);


tup2 = ('abc', 'xyz');
# Following action is not valid for tuples
# tup1[0] = 100;
# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;

Output:(12, 34.56, 'abc', 'xyz')


Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
TUPLES-Deleting tuple values

 Not possible
 However you can delete the whole
tuple.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
DICTIONARY

 Python Dictionary is used to store


the data in a key-value pair format.
 The dictionary is the data type in
Python, which can simulate the
real-life data arrangement where
some specific value exists for some
particular key.
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
DICTIONARY

 The dictionary can be created by using


multiple key-value pairs enclosed with the
curly brackets {}, and each key is separated
from its value by the colon (:).The syntax to
define the dictionary is given below.

 Dict = {"Name": "Tom", "Age": 22}

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
DICTIONARY-example
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGL
E"}
print(type(Employee))
print("printing Employee data .... ")
print(Employee)

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
DICTIONARY-Accessing values
We have discussed how the data can be accessed in the list and tuple by
using the indexing.

However, the values can be accessed in the dictionary by using the keys as
keys are unique in the dictionary.

The dictionary values can be accessed in the following way.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
DICTIONARY-Accessing values

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
SETS

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
SETS

A Python set is the collection of the unordered items.


Each element in the set must be unique, immutable, and
the sets remove the duplicate elements. Sets are
mutable which means we can modify it after its creation.

Unlike other collections in Python, there is no index


attached to the elements of the set, i.e., we cannot
directly access any element of the set by the index.
However, we can print them all together, or we can get
the list of elements by looping through the set.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
SETS-Creating a set.

The set can be created by enclosing the comma-


separated immutable items with the curly braces
{}. Python also provides the set() method, which
can be used to create the set by the passed
sequence.

Countries={“Zimbabwe”,”Malawi”,”Zambia”,”Bo
tswana”}
Group1=set([“Liverpool", “ManU", “ManCity", “Arsenal"
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
DICTIONARY

Updating and deleting-self


study
[Link]
python-dictionary

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
DICTIONARY- looping through
1. Printing the keys:

Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGL


E"}
for x in Employee:
print(x)

Output: Name
Age
salary
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
DICTIONARY- looping through
1. Printing the values:

Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGL


E"}
for x in Employee:
print(Employee[x])
Output: John
29
25000
GOOGLE
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
DICTIONARY- looping through
1. Printing both keys and values:

Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGL


E"}
for x in [Link]():
print(x)

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS

In programming, a function is a group of related


statements that performs a specific task.
Functions help break our program into smaller
and modular chunks.
As our program grows larger and larger,
functions make it more organized and
manageable.
Furthermore, it avoids repetition and makes the
code reusable.
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
FUNCTIONS

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
TYPES OF FUNCTIONS
Functions which come along with
the python language.
BUILT IN Also known as predefined
functions.
e.g print(),range(), split() etc

Functions which are created by


programmer explicitly according to
USER DEFINED the requirement are called a user-
defined function.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS

Syntax
def
function_name(parameters):

statement(s)

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS
def
function_name(parameters):

statement(s)
Keyword def that marks the start of the function
header

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS
def
function_name(parameters):

statement(s)
A function name to uniquely identify the
function. Function naming follows the same rules
of writing identifiers in Python.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS
def
function_name(parameters):

statement(s)
Parameters (arguments) through which we pass
values to a function. They are optional.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS
def
function_name(parameters):

statement(s)
A colon (:) to mark the end of the
function header.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS
def
function_name(parameters):

statement(s)
One or more valid python statements that make
up the function body. Statements must have the
same indentation level (usually 4 spaces).

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS
def
function_name(parameters):

statement(s)

An optional return statement to return


a value from the function.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS

def Greeting():
print("Hello World")

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS

Calling a function

def Greeting():
print("Hello World")
Greeting()
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS with parameters

def Greeting(name,):
print(f"Hello,
{name}")

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
FUNCTIONS with parameters

Calling a function with


parameters

def Greeting(name):
print(f"Hello World {name}")
Greeting(“Taurai”)
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
FUNCTIONS with parameters and
return value
Calling a function with
parameters
def Add(a,b):
sum=a+b
return sum
ans=Add(23,56)
Free SlideSalad PowerPoint Template
Copyright (C) [Link] All rights
reserved.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
MODULES

In Python, modules refer to the


Python file, which contains
Python code like Python
statements, classes,
functions, variables, etc. A
file with Python code is
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
MODULES

In Python, large code is divided


into small modules. The
benefit of modules is, it
provides a way to share
reusable functions.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
TYPES OF MODULES
Functions which come along with
the python language.
BUILT IN Also known as predefined
functions.
e.g print(),range(), split() etc

Functions which are created by


programmer explicitly according to
USER DEFINED the requirement are called a user-
defined function.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
BUILT IN
Built-in modules come with default Python installation.
One of Python’s most significant advantages is its rich
library support that contains lots of built-in modules.
Hence, it provides a lot of reusable code.

Some commonly used Python built-in modules are


datetime, os, math, sys, random, etc.

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
TYPES OF MODULES

USER DEFINED

The modules which the user defines or


create are called a user-defined module.
We can create our own module, which
contains classes, functions, variables, etc.,
as per our requirements.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
HOW TO USE MODULES

To import modules in Python, we use the


Python import keyword. With the help of
the import keyword, both the built-in and
user-defined modules are imported. Let’s
see an example of importing a math
module.
Copyright (C) [Link] All rights
Free SlideSalad PowerPoint Template
reserved.
HOW TO USE MODULES

At times we need to import specific


functions from a particular module:

from module_name import function


from math import sqrt

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
HOW TO USE MODULES

We can also rename a module of function


as we call it:

from module_name import function_name as new_name


from math import sqrt as s

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
HOW TO USE MODULES

We can also import all the functions from a


module:

We use the *
from math import*

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.
QUESTIONS

Copyright (C) [Link] All rights


Free SlideSalad PowerPoint Template
reserved.

You might also like