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

Python Data Types-Variables and Control structures

The document provides an overview of Python fundamentals, focusing on data types, operators, and control statements. It explains various data types such as numeric, string, and Boolean, along with their operations and examples. Additionally, it covers different types of operators, including arithmetic, comparison, and logical operators, as well as decision-making statements like if, else, and nested if statements.

Uploaded by

globallandmark11
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)
4 views

Python Data Types-Variables and Control structures

The document provides an overview of Python fundamentals, focusing on data types, operators, and control statements. It explains various data types such as numeric, string, and Boolean, along with their operations and examples. Additionally, it covers different types of operators, including arithmetic, comparison, and logical operators, as well as decision-making statements like if, else, and nested if statements.

Uploaded by

globallandmark11
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/ 87

Python Fundamentals

Python Data Types

• Python Data Types are used to define the type of a


variable. It defines what type of data we are going to store
in a variable.
• The data stored in memory can be of many types.
• For example, a person's age is stored as a numeric value and
his or her address is stored as alphanumeric characters
Python Data Types
Python Fundamentals
Python Data Types
• A data type represents a kind of value and determines
what operations can be done on it.
• Numeric, non-numeric and Boolean (true/false) data are
the most obvious data types. However, each programming
language has its own classification largely reflecting its
programming philosophy.
Python Fundamentals
Types of Python Data Types
Python Fundamentals
Data Types
Python Fundamentals
Python Numeric Data Type
• Python numeric data types store numeric values.
Number objects are created when you assign a value
to them.
• var1 = 1 # int data type
• var2 = True # bool data type
• var3 = 10.023 # float data type
• var4 = 10+3j # complex data type
Python Fundamentals
Python Numeric Data Type
• Python supports four different numerical types and
each of them have built-in classes in Python library,
called int, bool, float and complex respectively
• int (signed integers)
• float (floating point real values)
• complex (complex numbers)
Python Fundamentals
Example of Numeric Data Types
Python Fundamentals
Python String Data Type
• Python string is a sequence of one or more Unicode
characters, enclosed in single, double or triple quotation
marks (also called inverted commas).
• Python strings are immutable which means when you
perform an operation on strings, you always produce a
new string object of the same type, rather than mutating
an existing string
Python Fundamentals
Python String Data Type
• A string is a non-numeric data type. Obviously, we
cannot perform arithmetic operations on it. However,
operations such as slicing and concatenation can be
done. Python's str class defines a number of useful
methods for string processing.
• Subsets of strings can be taken using the slice operator ([
] and [:] ) with indexes starting at 0 in the beginning of
the string and working their way from -1 at the end.
Python Fundamentals
Python String Data Type

• The plus (+) sign is the string concatenation operator


and the asterisk (*) is the repetition operator in Python.
Python Fundamentals
Python Built in Functions
Python Fundamentals
Python Built in Functions
Python Fundamentals
Python Built in Functions
Python Fundamentals
Python Built in Functions
Python Fundamentals
Python Built in Functions
Python Fundamentals
Python –String Methods
Python Fundamentals
Python –String Methods
Python Fundamentals
Python –String Methods
Python Fundamentals
Python -Operators

• Operators are used to perform operations on variables and values


• Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python Fundamentals
Python Arithmetic Operators
• Arithmetic operators are used with numeric values to
perform common mathematical operations like:
Python Fundamentals
Python Arithmetic Operators
Addition Operator (+)
• This operator pronounced as plus, is a basic arithmetic
operator. It adds the two numeric operands on the either side
and returns the addition result.
• Example: Addition of Two Integers
• In the following example, the two integer variables are the
operands for the "+" operator.
a=10
b=20
print ("Addition of two integers")
print ("a =",a,"b =",b,"addition =",a+b)
Python Fundamentals
Python Arithmetic Operators
Subtraction Operator (-)
This operator, known as minus, subtracts the second operand
from the first. The resultant number is negative if the second
operand is larger.
• Example: Subtraction of Two Integers:
a=10 ,b=20
print ("Subtraction of two integers:")
print ("a =",a,"b =",b,"a-b =",a-b) print ("a =",a,"b =",b,"b-a =",b-a)
Python Fundamentals
Python Arithmetic Operators
Multiplication Operator (*)
• The * (asterisk) symbol is defined as a multiplication operator
in Python (as in many languages).
• It returns the product of the two operands on its either side.
• If any of the operands negative, the result is also negative. If
both are negative, the result is positive.
• Changing the order of operands doesn't change the result
Python Fundamentals
Python Arithmetic Operators
Multiplication Operator (*)
• Examples
a=10
b=20
print ("Multiplication of two integers")
print ("a =",a,"b =",b,"a*b =",a*b)
Python Fundamentals
Python Arithmetic Operators
Multiplication Operator (*)
• Examples
a=10 b=20.5
print ("Multiplication of integer and float")
print ("a=",a,"b=",b,"a*b=",a*b)
a=-5.55
b=6.75E-3
print ("Multiplication of float and float")
print ("a =",a,"b =",b,"a*b =",a*b)
Python Fundamentals
Python Arithmetic Operators
Multiplication Operator (*)
• Examples
a=10+5j
b=20.5
print ("Multiplication of complex and float")
print ("a =",a,"b =",b,"a*b =",a*b)
Python Fundamentals
Python Arithmetic Operators
Division Operator (/)
• The "/" symbol is usually called as forward slash.
• The result of division operator is numerator (left operand)
divided by denominator (right operand).
• The resultant number is negative if any of the operands is
negative.
• Since infinity cannot be stored in the memory, Python raises
ZeroDivisionError if the denominator is 0
Python Fundamentals
Python Arithmetic Operators
Division Operator (/)
• Examples
a=10
b=20
print ("Division of two integers")
print ("a=",a,"b=",b,"a/b=",a/b)
print ("a=",a,"b=",b,"b/a=",b/a)
Python Fundamentals
Python Arithmetic Operators
Division Operator (/)
• Examples
a=10
b=-20.5
print ("Division of integer and float")
print ("a=",a,"b=",b,"a/b=",a/b)
a=-2.50
b=1.25E2
print ("Division of float and float")
print ("a=",a,"b=",b,"a/b=",a/b)
Python Fundamentals
Python Arithmetic Operators
Modulus Operator (%)
• Python defines the "%" symbol, which is known aa Percent
symbol, as Modulus (or modulo) operator.
• It returns the remainder after the denominator divides the
numerator. It can also be called Remainder operator.
• The result of the modulus operator is the number that
remains after the integer quotient
Python Fundamentals
Python Arithmetic Operators
Modulus Operator (%)
• Examples:
a=10 b=2
print ("a=",a, "b=",b, "a%b=", a%b)
a=10 b=4
print ("a=",a, "b=",b, "a%b=", a%b)
print ("a=",a, "b=",b, "b%a=", b%a) a=0 b=10
print ("a=",a, "b=",b, "a%b=", a%b)
print ("a=", a, "b=", b, "b%a=",b%a)
Python Fundamentals
Python –Exponent Operator (**)

• Python uses ** (double asterisk) as the exponent operator


(sometimes called raised to operator).
• So, for a**b, you say a raised to b, or even bth power of a.
• If in the exponentiation expression, both operands are
integer, result is also an integer.
• In case either one is a float, the result is float.
• Similarly, if either one operand is complex number,
exponent operator returns a complex number.
Python Fundamentals
Python –Exponent Operator (**)

a=10 b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=10 b=1.5
print ("a=",a, "b=",b, "a**b=", a**b)
a=7.7 b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=1+2j b=4
print ("a=",a, "b=",b, "a**b=", a**b) a=12.4 b=0
print ("a=",a, "b=",b, "a**b=", a**b) print ("a=",a, "b=",b, "b**a=",
b**a)
Python Fundamentals
Python –Operators
Floor Division Operator (//)
• Floor division is also called as integer division.
Python uses // (double forward slash) symbol for
the purpose.
• Unlike the modulus or modulo which returns the
remainder, the floor division gives the quotient of
the division of operands involved.
Python Fundamentals
Python –Operators
Floor Division Operator (//)
• Example:
a=9 b=2
print ("a=",a, "b=",b, "a//b=", a//b)
a=9 b=-2
print ("a=",a, "b=",b, "a//b=", a//b)
a=10 b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
a=-10 b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
Python Fundamentals
Python –Operators
Python Fundamentals
Python –Python Comparison Operators

• Comparison operators in Python are very important


in Python's conditional statements (if, else and elif)
and looping statements (while and for loops).
The comparison operators also called relational operators.
Some of the well known operators are "<" stands for less
than, and ">" stands for greater than operator.
Python Fundamentals
Python –Python Comparison Operators
Python Fundamentals
Python –Python Comparison Operators

• Example
print ("Both operands are integer")
a=5
b=7
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
Python Fundamentals
Python - Assignment Operators

• The = (equal to) symbol is defined as assignment


operator in Python.
• The value of Python expression on its right is assigned to
a single variable on its left.
• The = symbol as in programming in general (and Python
in particular) should not be confused with its usage in
Mathematics, where it states that the expressions on the
either side of the symbol are equal.
Python Fundamentals
Python - Assignment Operators

• Example
a = 10
b=5
a=a+b
print (a)
In the statement "a+=b", the two operators "+" and "=" can be
combined in a "+=" operator.
It is called as add and assign operator. In a single statement, it
performs addition of two operands "a" and "b", and result is assigned
to operand on left, i.e., "a".
Python Fundamentals
Python - Assignment Operators

• Augmented Assignment Operators in Python


In addition to the simple assignment operator, Python provides
few more assignment operators for advanced use.
They are called cumulative or augmented assignment operators.
In this chapter, we shall learn to use augmented assignment
operators defined in Python.
Python Fundamentals
Python - Assignment Operators
Python Fundamentals
Python - Assignment Operators
Python Fundamentals
Python - Assignment Operators
Python Fundamentals
Python - Assignment Operators
Python Fundamentals
Python - Logical Operators

• Python logical operators are used to form compound


Boolean expressions. Each operand for these logical
operators is itself a Boolean expression
Python Fundamentals
Python - Logical Operators
• Logical "and" Operator
For the compound Boolean expression to be True,
both the operands must be True. If any or both
operands evaluate to False, the expression returns False.
Python Fundamentals
Python - Logical Operators
• Logical “NOT" Operator
This is a unary operator. The state of Boolean operand that
follows, is reversed. As a result, not True becomes False and
not False becomes True
Python Fundamentals
Python Control Statements

• Python program control flow is regulated by various


types of conditional statements, loops,
and function calls.
• By default, the instructions in a computer program are
executed in a sequential manner, from top to bottom, or
from start to end.
• However, such sequentially executing programs can
perform only simplistic tasks.
Python Fundamentals
Python Control Statements
Decision Making Statements
• Decision making statements are used in the Python
programs to make them able to decide which of the
alternative group of instructions to be executed, depending
on value of a certain Boolean expression.
Python Fundamentals
Python Control Statements
• Python - Decision Making
Decision structures evaluate multiple expressions which produce
TRUE or FALSE as outcome. You need to determine which
action to take and which statements to execute if outcome is
TRUE or FALSE otherwise.
Python Fundamentals
Python Control Statements
• Decision Making Statements in Python
Python Fundamentals
Python Control Statements
• If Statement
Syntax of the if Statement
if expression:
# statement(s) to be executed

• If the boolean expression


evaluates to TRUE, then the
statement(s) inside the if block is
executed.
• If boolean expression evaluates to
FALSE, then the first set of code
after the end of the if block is
executed.
Python Fundamentals
Python Control Statements
If Statement
• Let us consider an example of a
customer entitled to 10% discount if
his purchase amount is > 1000; if not,
then no discount is applicable.
• The following flowchart shows the
whole decision making process
Python Fundamentals
Python Control Statements
• Single Statement Suites
If the suite of an if clause consists only of a single line, it may go
on the same line as the header statement.
var = 100
if ( var == 100 ) : print ("Value of expression is 100")
print ("Good bye!")
Python Fundamentals
Python Control Statements
• if...else statement
In this decision making statement, if the if condition is true, then
the statements within this block are executed, otherwise,
the else block is executed. The program will choose which block
of code to execute based on whether the condition in
the if statement is true or false.
Python Fundamentals
Python Control Statements
• if...else statement
If the boolean expression
evaluates to TRUE, then the
statement(s) inside the if
block will be executed
otherwise statements of
the else block will be executed
Python Fundamentals
Python Control Statements
• Python if-else Statement Example
Let us understand the use of if-else statements with the
following example. Here, variable age can take different
values. If the expression age > 18 is true, then eligible to
vote message will be displayed otherwise not eligible to
vote message will be displayed. Following flowchart illustrates
this logic
Python Fundamentals
Python Control Statements

Python Fundamentals
Python Control Statements
• Python if elif else Statement
• The if elif else statement allows you to check
multiple expressions for TRUE and execute a block
of code as soon as one of the conditions evaluates
to TRUE.
• Similar to the else block, the elif block is also
optional. However, a program can contains only
one else block whereas there can be an arbitrary
number of elif blocks following an if block.
Python Fundamentals
Python Control Statements
• Example
• 20% on amount exceeding 10000,

• 10% for amount between 5-10000,

• 5% if it is between 1 to 5000.

• no discount if amount<1000
Python Fundamentals
Python Control Statements
Nested if statements
• Python supports nested if statements which means we can
use a conditional if and if...else statement inside an existing if
statement.
• There may be a situation when you want to check for
additional conditions after the initial one resolves to true. In
such a situation, you can use the nested if construct.
• Additionally, within a nested if construct, you can include
an if...elif...else construct inside
another if...elif...else construct.
Python Fundamentals
Python Control Statements
Nested if statements-Syntax of Nested if Statement
Python Fundamentals
Python Control Statements
• Nested if Statement with else Condition-Syntax
Python Fundamentals
Python Control Statements
Python match-case Statement-Syntax
• Python match-case statement takes an expression and compares its value
to successive patterns given as one or more case blocks.
• Only the first pattern that matches gets executed.
• It is also possible to extract components (sequence elements or object
attributes) from the value into variables.
Python Fundamentals
Python Control Statements
Example
Hello, World!
Hello, World!

Python Fundamentals
Python –Loops

• Python loops allow us to execute a statement or group


of statements multiple times.
• In general, statements are executed sequentially: The
first statement in a function is executed first, followed
by the second, and so on.
• There may be a situation when you need to execute a
block of code several number of times.
Hello, World!
Hello, World!

Python Fundamentals
Python –Loops

• Flowchart of a Loop
Hello, World!
Hello, World!

Python Fundamentals
Python –Loops

• Types of Loops in Python


Python Fundamentals
Python –Loops
Python Loop Control Statements
• Loop control statements change execution from its normal sequence.
When execution leaves a scope, all automatic objects that were created in
that scope are destroyed.
• Python supports the following control statements
Python Fundamentals
Python –For Loops
• The for loop in Python provides the ability to loop over the
items of any sequence, such as a list, tuple or a string.
• It performs the same action on each item of the sequence.
This loop starts with the for keyword, followed by a variable
that represents the current item in the sequence.
• The in keyword links the variable to the sequence you want
to iterate over.
• A colon (:) is used at the end of the loop header, and the
indented block of code beneath it is executed once for each
item in the sequence
Python Fundamentals
Python –For Loops
• Flowchart of Python for Loop
Python Fundamentals
Python –For Loops
Example
• In the following example, the for loop traverses a tuple
containing integers and returns the total of all numbers
Python Fundamentals
Python –For Loops
Example
• In the following example, the for loop traverses a list
containing integers and prints only those which are divisible
by 2
Python Fundamentals
Python for Loop with Range Objects

• Python's built-in range() function returns an iterator object


that streams a sequence of numbers.
• This object contains integers from start to stop, separated
by step parameter. You can run a for loop with range as well
Syntax:
Using else Statement with For Loop

Python supports to have an else statement associated with


a loop statement. However, the else statement is executed
when the loop has exhausted iterating the list.
• Example
The following example illustrates the combination of an
else statement with a for statement that searches for prime
numbers from 10 to 20.
Using else Statement with For Loop
Python Fundamentals
Python –While Loops

• A while loop in Python programming language repeatedly


executes a target statement as long as the specified boolean
expression is true.
• This loop starts with while keyword followed by a boolean
expression and colon symbol (:). Then, an indented block of
statements starts.
• Here, statement(s) may be a single statement or a block of
statements with uniform indent.
• The condition may be any expression, and true is any non-
zero value. As soon as the expression becomes false, the
program control passes to the line immediately following the
loop.
Python Fundamentals
Python –While Loops
Syntax of while Loop

Flowchart of While loop


Python Fundamentals
Python –While Loops
Example 1
• The following example illustrates the working of while loop.
Here, the iteration run till value of count will become 5.
Python Fundamentals
Python –While Loops
Example 2
• Here is another example of using the while loop. For each
iteration, the program asks for user input and keeps repeating
till the user inputs a non-numeric string. The isnumeric()
function returns true if input is an integer, false otherwise.
Python Fundamentals
Python –Nested Loops
• In Python, when you write one or more loops within a loop
statement that is known as a nested loop.
• The main loop is considered as outer loop and loop(s) inside
the outer loop are known as inner loops.
• The Python programming language allows the use of one loop
inside another loop.
• A loop is a code block that executes specific instructions
repeatedly.
• There are two types of loops, namely for and while, using
which we can create nested loops.
Python Fundamentals
Python –Nested Loops
• Syntax: Nested for loop

• Example
Python Fundamentals
Python –Nested Loops
• Syntax: Nested while loop

• Example
QUESTIONS?

You might also like