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

Lecture 2@IntroductioToPython

Uploaded by

hudaisa wazir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 2@IntroductioToPython

Uploaded by

hudaisa wazir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to

Python

Ghulam Abbas
Programming Language
 A programming language is a notation for writing programs.

 A programming language is a computer language designed to

communicate instructions to a machine, particularly a computer.

 Programming languages can be used to create programs, to control the

behaviour of a machine or to express algorithms.

 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

 Dynamic programming language

 Open source model


WHY PYTHON ?
Python is nowadays widely used as an programming language. It has :-

 Simple syntax

 One-Line command

 English like commands(easily readable)

 Dynamically Typed

 Easily Indentable

 Built-in data structures like tuples, sets, dictionaries,strings,lists etc


Applications of Python language
Python language used in:
– Web Development

– 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

 MULTIPLE ASSIGNMENT: In Python, multiple


assignments can be made in a single statement.

 We can assign the same value to multiple variables at once.


NUMBERS
 Number data types store numeric values.

 They are immutable data types.

 Number objects are created when you assign a value to them.

 We can delete the reference to a number object by using the del statement.

 Syntax: del var1[,var2[,var3[...., varN]]]]

 We can delete a single object or multiple objects by using the del


statement.
NUMBERS
 Integer numbers: Integers can be of any length, it is only limited by the
memory available. They are positive or negative whole numbers with no
decimal point.
 Floating point number : It is accurate up to 15 decimal places.
 Complex numbers : These are written in the form, x + yj, where x is
the real part and y is the imaginary part.
 Examples:
Integer
no:

Floating point
no:
Complex
number
EXAMPLES OF NUMBER
 In interactive mode, the last printed expression is assigned to the variable
_
 Example:

 Division (/) always returns a float.

 To get an integer result, use floor


division (//)

 To calculate the remainder you can use %:


NUMBERS
 Abs(): returns the absolute value of x i.e. the positive distance between x and
zero.

 Ceil() : returns the ceiling value of x i.e. the smallest integer not less than x.

 EXP(): returns exponential of x: (e power


x).

 Fabs(): returns the absolute value of x. fabs() is defined in math module


and works only on float and integer number.
NUMBERS
• Floor(): returns the floor of x i.e. the largest integer not greater
than x.

• Log(): returns the natural logarithm of x.

• Log10(): returns base-10 logarithm of x for x > 0.

• Max(): returns the largest of its arguments

• There are many more functions that perform mathematical operations


on numbers.
Control Flow And Loops
Various control statements are:
 if and else

 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

they appear in the sequence.

 Syntax:

for i in m:

// repeat body for each item in m


Examples of For statement
Example1: Example 2:

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

execution at the next statement , just like break statement in c.

 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

You might also like