Lecture 2@IntroductioToPython
Lecture 2@IntroductioToPython
Python
Ghulam Abbas
Programming Language
A programming language is a notation for writing programs.
Various programming languages are such as: c , c++ , R, java, C# , ruby , python
etc.
What is Python ?
Python is a high level programming language which is:
Interpreted
Interactive
Object-Oriented
Simple syntax
One-Line command
Dynamically Typed
Easily Indentable
– Database programming
– Game Development
– Scientific Computing
– Web Framework
– Molecular Biology
PYTHON VARIABLES
A variable is a location in memory used to store some data.
They are given unique names to differentiate between different memory locations.
Don't need to declare a variable before using it.
In Python, simply assign a value to a variable and it will exist. Don’t declare the type of
the variable.
VARIABLE ASSIGNMENT: We use the assignment operator (=) to assign values to a
variable.
Assignment
operator
We can delete the reference to a number object by using the del statement.
Floating point
no:
Complex
number
EXAMPLES OF NUMBER
In interactive mode, the last printed expression is assigned to the variable
_
Example:
Ceil() : returns the ceiling value of x i.e. the smallest integer not less than x.
Elif
For
Range
While
Break
Continue
IF AND ELSE STATEMENT
• The syntax for if statement in python are:
• For
example: if #execution of an if statement
(condition):
stat
else: em #execution of an else statement
ent
1statement1
stat statement2
em
ent
• If we use else statement without using if statement then it will raise an error.
2
Example of If And Else
Output:
Elif statement
• Elif statement is similar to the else if statement used in c or c++.
• Elif statement is a combination of else and if statement.
• Syntax: if (condition):
statement1
statement2
elif (condition): # elif is a combination of else if
statement3
statement4
else:
• There can statement5
be zero or more elif parts, and the else part is optional. The keyword
‘elif ‘ is short for ‘else if’, and is useful to avoid excessive indentation.
Example of Elif statement
Output:
For Statement
The for statement in Python differs from in C or C++.
Python’s for statement iterates over the items of any sequence , in the order that
Syntax:
for i in m:
Output: Output
else with for loop
# program to check if an array
consists of even number
List1= [1, 9, 8] In the following example, the
List2= [1, 3, 5] else statement will only be
for ele in List1 :
if ele % 2 == 0: executed if no element of the
print ("list contains an even array is even, i.e. if statement
number")
break has not been executed for any
# This else executes only if break is
iteration. Therefore for the array
NEVER [1, 9, 8] the if is executed in the
# reached and loop terminated after all
iterations.
third iteration of the loop and
else: hence the else present after the
print ("list does not contain an
even number") for loop is ignored. In the case of
array [1, 3, 5] the if is not
executed for any iteration and
Range Function
Range function is used If we do not want to iterate over a sequence of numbers.
Range function produces the sequences of values ie i,i+1,……….j-1 if it
range(i,j).
If range(i,j,k) then it increments by k that is i, i+k,……………………….,i+nk.
Sequence produced by a range is not a list, use list(range(..)) to get a list.
Why does range(i ,j) stops at j-1?
to make it easier to process lists
To iterate over the indices of a sequence, you can combine range() and len() as
follows:
Examples Of Range Function
WHILE LOOP
• While loop in Python is used to iterate over a block of code as long as the test
expression(condition) is true.
• Syntax: while condition:
statement(s) //repeat body till condition
• Loop might not ever run:becomes
When thefalse
condition is tested and the result is false then
the loop body will be skipped and the first statement after the while loop is executed.
• Example:
output:
Break Statement
The break statement in python terminates the current loop and resumes
The break statement can be used in both for and while loops.
Output:
Continue Statement
• Continue statement in Python returns the control to the beginning of the while or
for loop. The continue statement rejects all the remaining statement in the
current iteration of the loop and moves the control back to the top of the loop.
Output:
Pass Statement
The pass statement is used in Python when statement is required but you do
not want any command or code to execute.
The pass statement is a null operation means nothing happens when it executes.
Suppose we have a loop or function that is not implemented yet , but we want to
implement it in future. They cannot have an empty body.
Pass statement is there to create minimal classes.
It is useful when you have created a code block but it is no longer required.
Comment statement is ignored by interpreter entirely and pass is not ignored.
Example: output