Unit Ii
Unit Ii
Python Variables
. Variable is a name that is used to refer to memory location. Python variable is also
known as an identifier and used to hold value.
. In Python, we don't need to specify the type of variable because Python is a infer
language and smart enough to get variable type.
. Variable names can be agroup of both the letters and digits, but they have to begin
with a letter or an underscore.
. It is recommended to use lowercase letters for the variable name. Rahulandrahul both
are two different variables.
Identifier Naming
Variables are the example of identifiers. An Identifier is used to identify the literals used in the
program. The rules to name an identifier are given below.
Python does not bind us to declare a variable before using it in the application. It allows us to
create a variable at the required time.
We don't need to declare explicitly variable in Python. When we assign any value to the
variable, that variable is declared automatically.
SRINIVASARAO G (PROF)-VITAP 1
PROBLEM SOLVING USING PYTHON
Object References
It is necessary to understand how the Python interpreter works when we declare a variable. The
process of treating variables is somewhat different from many other programming languages.
Python is the highly object-oriented programming language; that's why every data item belongs
to a specific type of class. Consider the following example.
print("John")
Output:
John
The Python object creates an integer object and displays it to the console. In the above print
statement, we have created a string object. Let's check the type of it using the Python built-in
type() function.
1. type("John")
Output:
<class 'str'>
In Python, variables are a symbolic name that is a reference or pointer to an object. The variables
are used to denote objects by that name.
1. a = 50
a = 50
b=a
SRINIVASARAO G (PROF)-VITAP 2
PROBLEM SOLVING USING PYTHON
The variable b refers to the same object that a points to because Python does not create another
object.
Let's assign the new value to b. Now both variables will refer to the different objects.
a = 50
b =100
Python manages memory efficiently if we assign the same variable to two different values.
Object Identity
In Python, every created object identifies uniquely in Python. Python provides the guaranteed that
no two objects will have the same identifier. The built-in id() function, is used to identify the
object identifier. Consider the following example.
1. a = 50
2. b = a
3. print(id(a))
4. print(id(b))
5. # Reassigned variable a
6. a = 500
SRINIVASARAO G (PROF)-VITAP 3
PROBLEM SOLVING USING PYTHON
7. print(id(a))
SRINIVASARAO G (PROF)-VITAP 3
PROBLEM SOLVING USING PYTHON
Output:
140734982691168
140734982691168
2822056960944
We assigned the b = a, a and b both point to the same object. When we checked by the id()
function it returned the same number. We reassign a to 500; then it referred to the new object
identifier.
Variable Names
We have already discussed how to declare the valid variable. Variable names can be any length
can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_).
Consider the following example of valid variables names
1. name = "Devansh"
2. age = 20
3. marks = 80.50
4. print(name)
5. print(age)
6. print(marks)
Output:
Devansh
20
80.5
1. name = "A"
2. Name = "B"
3. naMe = "C"
4. NAME = "D"
5. n a m_e = "E"
6. _name = "F"
7. name_ = "G"
8. _name_ = "H"
SRINIVASARAO G (PROF)-VITAP 4
PROBLEM SOLVING USING PYTHON
9. na56me = "I"
10. print(name,Name,naMe,NAME,n a m_e, NAME, n a m_e, _name, name , name,
na56me)
Output:
ABCDEDEFGFI
o Camel Case - In the camel case, each word or abbreviation in the middle of begins with a
capital letter. There is no intervention of whitespace. For example - nameOfStudent,
valueOfVaraible, etc.
o Pascal Case - It is the same as the Camel Case, but here the first word is also capital.
For example - NameOfStudent, etc.
o Snake Case - In the snake case, Words are separated by the underscore. For
example - name_of_student, etc.
Multiple Assignment
Python allows us to assign a value to multiple variables in a single statement, which is also
known as multiple assignments.
We can apply multiple assignments in two ways, either by assigning a single value to multiple
variables or assigning multiple values to multiple variables. Consider the following example.
Eg:
1. x=y=z=50
2. print(x)
3. print(y)
4. print(z)
Output:
50
50
50
SRINIVASARAO G (PROF)-VITAP 5
PROBLEM SOLVING USING PYTHON
Eg:
1. a,b,c=5,10,15
2. print a
3. print b
4. print c
Output:
5
10
15
The values will be assigned in the order in which variables appear.
There are two types of variables in Python - Local variable and Global variable. Let's understand
the following variables.
Local Variable
Local variables are the variables that declared inside the function and have scope within the
function. Let's understand the following example.
Example -
1. # Declaring a function
2. def add():
3. # Defining local variables. They has scope only within a function
4. a = 20
5. b = 30
6. c=a+b
7. print("The sum is:", c)
8. # Calling a function
9. add()
SRINIVASARAO G (PROF)-VITAP 6
PROBLEM SOLVING USING PYTHON
Output:
In the above code, we declared a function named add() and assigned a few variables within the
function. These variables will be referred to as the local variables which have scope only inside
the function. If we try to use them outside the function, we get a following error.
1. add()
2. # Accessing local variable outside the function
3. print(a)
Output:
Global Variables
Global variables can be used throughout the program, and its scope is in the entire program. We
can use global variables inside or outside the function.
A variable declared outside the function is the global variable by default. Python
provides the global keyword to use global variable inside the function. If we don't use the global
keyword, the function treats it as a local variable. Let's understand the following example.
Example -
SRINIVASARAO G (PROF)-VITAP 7
PROBLEM SOLVING USING PYTHON
12. print(x)
Output:
101
Welcome To Javatpoint
Welcome To Javatpoint
Explanation:
In the above code, we declare a global variable x and assign a value to it. Next, we defined a
function and accessed the declared variable using the global keyword inside the function. Now we
can modify its value. Then, we assigned a new string value to the variable x.
Now, we called the function and proceeded to print x. It printed the as newly assigned value of x.
Delete a variable
We can delete the variable using the del keyword. The syntax is given below.
1. del <variable_name>
In the following example, we create a variable x and assign value to it. We deleted variable x,
and print it, we get the error "variable x is not defined". The variable x will no longer use in
future.
Example -
1. # Assigning a value to x
2. x = 6
3. print(x)
4. # deleting a variable.
5. del x
6. print(x)
Output:
SRINIVASARAO G (PROF)-VITAP 8
PROBLEM SOLVING USING PYTHON
6
Traceback (most recent call last):
File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/multiprocessing.py", line 389, in
print(x)
NameError: name 'x' is not defined
SRINIVASARAO G (PROF)-VITAP 8
PROBLEM SOLVING USING PYTHON
We can print multiple variables within the single print statement. Below are the example of single
and multiple printing values.
Output:
5
5
Example - 2 (Printing Multiple Variables)
1. a = 5
2. b = 6
3. # printing multiple variables
4. print(a,b)
5. # separate the variables by the comma
6. Print(1, 2, 3, 4, 5, 6, 7, 8)
Output:
56
12345678
SRINIVASARAO G (PROF)-VITAP 9
PROBLEM SOLVING USING PYTHON
1. a = 5
The variable a holds integer value five and we did not define its type. Python interpreter will
automatically interpret variables a as an integer type.
Python enables us to check the type of the variable used in the program. Python provides
us the type() function, which returns the type of the variable passed.
Consider the following example to define the values of different data types and checking its type.
1. a=10
2. b="Hi Python"
3. c = 10.5
4. print(type(a))
5. print(type(b))
6. print(type(c))
Output:
<type 'int'>
<type 'str'>
<type 'float'>
A variable can hold different types of values. For example,a person's name must be stored as a
string whereas its id must be stored as an integer.
Python provides various standard data types that define the storage method on each of them. The
data types defined in Python are given below.
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
SRINIVASARAO G (PROF)-VITAP 10
PROBLEM SOLVING USING PYTHON
Numbers
Number stores numeric values. The integer, float, and complex values belong to a Python
Numbers data- type. Python provides the type() function to know the data-type of the
variable. Similarly, the isinstance() function is used to check an object belongs to a particular
class.
Python creates Number objects when a number is assigned to a variable. For example;
1. a = 5
2. print("The type of a", type(a))
3. b = 40.5
4. print("The type of b", type(b))
5. c = 1+3j
6. print("The type of c", type(c))
7. print(" c is a complex number", isinstance(1+3j,complex))
Output:
1. Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc. Python
has no restriction on the length of an integer. Its value belongs to int
2. Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is accurate
upto
15 decimal points.
3. complex - A complex number contains an ordered pair, i.e., x + iy where x andy denote
the real and imaginary parts, respectively. The complex numbers like 2.14j, 2.0 + 2.3j, etc.
Sequence Type
String
The string can be defined as the sequence of characters represented in the quotation marks. In
Python, we can use single, double, or triple quotes to define a string.
SRINIVASARAO G (PROF)-VITAP 11
PROBLEM SOLVING USING PYTHON
String handling in Python is a straightforward task since Python provides built-in functions and
operators to perform operations in the string.
In the case of string handling, the operator + is used to concatenate two strings as the operation
"hello"+" python" returns "hello python".
The operator * is known as a repetition operator as the operation "Python" *2 returns 'Python
Python'.
Example - 1
Output:
Example - 2
Output:
he
o
hello javatpointhello javatpoint
hello javatpoint how are you
SRINIVASARAO G (PROF)-VITAP 12
PROBLEM SOLVING USING PYTHON
List
Python Lists are similar to arrays in C. However, the list can contain data of different types. The
items stored in the list are separated with a comma (,) and enclosed within square brackets [].
We can use slice [:] operators to access the data of the list. The concatenation operator (+) and
repetition operator (*) works with the list in the same way as they were working with the strings.
Output:
SRINIVASARAO G (PROF)-VITAP 13
PROBLEM SOLVING USING PYTHON
Tuple
A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of the
items of different data types. The items of the tuple are separated with a comma (,) and enclosed
in parentheses ().
A tuple is a read-only data structure as we can't modify the size and value of the items of a tuple.
Output:
<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
SRINIVASARAO G (PROF)-VITAP 14
PROBLEM SOLVING USING PYTHON
Dictionary
Dictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash
table where each key stores aspecific value. Key can hold any primitive data type, whereas value
is an arbitrary Python object.
The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}.
Output:
Boolean
Boolean type provides two built-in values, True and False. These values are used to determine the
given statement true or false. It denotes by the class bool. True can be represented by any non-
zero value or 'T'whereas false can be represented by the 0 or 'F'. Consider the following example.
SRINIVASARAO G (PROF)-VITAP 15
PROBLEM SOLVING USING PYTHON
Output:
<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined
Set
Python Set is the unordered collection of the data type. It is iterable, mutable(can modify after
creation), and has unique elements. In set, the order of the elements is undefined; it may
return the changed sequence of the element. The set is created by using a built-in function set(),
or asequence of elements is passed in the curly braces and separated by the comma. It can
contain various types of values. Consider the following example.
Output:
SRINIVASARAO G (PROF)-VITAP 16
PROBLEM SOLVING USING PYTHON
Python Operators
The operator can be defined as a symbol which is responsible for a particular operation between
two operands. Operators are the pillars of a program on which the logic is built in a specific
programming language. Python provides a variety of operators, which are described as follows.
o Arithmetic operators
o Comparison operators
o Assignment Operators
o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations between two operands. It includes
+ (addition), - (subtraction), *(multiplication), /(divide), %(reminder), //(floor division), and
exponent (**) operators.
Consider the following table for a detailed explanation of arithmetic operators.
Operator Description
+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30
- (Subtraction) It is used to subtract the second operand from the first operand. If the first
operand is less than the second operand, the value results negative. For
example, if a = 20, b = 10 => a - b = 10k
/ (divide) It returns the quotient after dividing the first operand by the second operand.
For example, if a = 20, b = 10 => a/b = 2.0
*
It is used to multiply one operand with the other. For example, if a = 20, b = 10
(Multiplication) => a * b = 200
SRINIVASARAO G (PROF)-VITAP 17
PROBLEM SOLVING USING PYTHON
% (reminder) It returns the reminder after dividing the first operand by the second operand.
For example, if a = 20, b = 10 => a%b = 0
// (Floor It gives the floor value of the quotient produced by dividing the two operands.
division)
Comparison operator
Comparison operators are used to comparing the value of the two operands and returns Boolean
true or false accordingly. The comparison operators are described in the following table.
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition
becomes true.
>= If the first operand is greater than or equal to the second operand, then the condition
becomes true.
> If the first operand is greater than the second operand, then the condition becomes true.
< If the first operand is less than the second operand, then the condition becomes true.
Assignment Operators]
The assignment operators are used to assign the value of the right expression to the left
operand. The assignment operators are described in the following table.
Operator Description
SRINIVASARAO G (PROF)-VITAP 18
PROBLEM SOLVING USING PYTHON
+= It increases the value of the left operand by the value of the right operand and assigns the
modified value back to left operand. For example, if a = 10, b = 20 => a+ = b will be
equal to a = a+ band therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and assigns the
modified value back to left operand. For example, if a = 20, b = 10 => a- = b will be equal
to a = a- band therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and assigns
the modified value back to then the left operand. For example, if a = 10, b = 20 =>
a* = bwill be equal to a = a* band therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and assigns the
reminder back to the left operand. For example, if a = 20, b = 10 => a % = b will be equal
to a = a % band therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=bwill assign 4**2 = 16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=bwill assign 4//3 = 1 to a.
Bitwise Operators
The bitwise operators perform bit by bit operation on the values of the two operands. Consider
the following example.
For example,
1. if a = 7
2. b=6
3. then, binary (a) = 0111
4. binary (b) = 0110
5. hence, a & b = 0110
6. a | b = 0111
7. a ^ b = 0001
8. ~ a = 1000
SRINIVASARAO G (PROF)-VITAP 19
PROBLEM SOLVING USING PYTHON
Operator Description
& (binary If both the bits at the same place in two operands are 1, then 1 is copied to the
and) result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be
1.
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the resulting bit
will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the
resulting bit will be 1 and viceversa.
<< (left shift) The left operand value is moved left by the number of bits present in the right
operand.
>> (right The left operand is moved right by the number of bits present in the right operand.
shift)
Logical Operators
The logical operators are used primarily in the expression evaluation to make a decision. Python
supports the following logical operators.
Operator Description
and If both the expression are true, then the condition will be true. If a and bare
the two expressions, a → true, b → true => aand b → true.
or If one of the expressions is true, then the condition will be true. If a and bare
the two expressions, a → true, b → false => a orb → true.
not If an expression a is true, then not (a) will be false and viceversa.
SRINIVASARAO G (PROF)-VITAP 20
PROBLEM SOLVING USING PYTHON
Membership Operators
Python membership operators are used to check the membership of value inside a Python data
structure. If the value is present in the data structure, then the resulting value is true otherwise it
returns false.
Operator Description
in It is evaluated to be true if the first operand is found in the second operand (list,
tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand (list,
tuple, or dictionary).
Identity Operators
The identity operators are used to decide whether an element certain class or type.
Operator Description
is It is evaluated to be true if the reference present at both sides point to the same object.
is not It is evaluated to be true if the reference present at both sides do not point to the same
object.
SRINIVASARAO G (PROF)-VITAP 21
PROBLEM SOLVING USING PYTHON
Branching Statements
. Decision making is the most important aspect of almost all the programming languages.
. As the name implies, decision making allows us to run a particular block of code for a
particular decision.
. Here, the decisions are made on the validity of the particular conditions.
. Condition checking is the backbone of decision making.
Statement Description
If Statement The if statement is used to test a specific condition. If the condition is true,a block of code (if-
block) will be executed.
If - The if-else statement is similar to if statement except the fact that, it also provides the block of
else Statement the code for the false case of the condition to be checked. If the condition provided in the if
statement is false, then the else statement will be executed.
Nested if Nested if statements enable us to use if? else statement inside an outer if statement.
Statement
Indentation in Python
. For the ease of programming and to achieve simplicity, python doesn't allow the
use of parentheses for the block level code.
. In Python, indentation is used to declare a block. If two statements are at the same
indentation level, then they are the part of the same block.
. Generally, four spaces are given to indent the statements which are a typical
amount of indentation in python.
. Indentation is the most used part of the python language since it declares the block of
code. All the statements of one block are intended at the same level indentation. We will
see how the actual indentation takes place in decision making and other stuff in python.
SRINIVASARAO G (PROF)-VITAP 22
PROBLEM SOLVING USING PYTHON
The if statement
. The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if-block.
. The condition of if statement can be any valid logical expression which can be either
evaluated to true or false.
1. if expression:
2. statement
Example 1
1. num = int(input("enter the number?"))
2. if num%2 == 0:
3. print("Number is even")
Output:
SRINIVASARAO G (PROF)-VITAP 23
PROBLEM SOLVING USING PYTHON
Output:
Enter a? 100
Enter b? 120
Enter c? 130
c is largest
1. if condition:
2. #block of statements
3. else:
4. #another block of statements (else-block)
SRINIVASARAO G (PROF)-VITAP 24
PROBLEM SOLVING USING PYTHON
if age>=18:
print("You are eligible to vote !!");
else:
print("Sorry!you have to wait !!");
Output:
SRINIVASARAO G (PROF)-VITAP 25
PROBLEM SOLVING USING PYTHON
Output:
2. # block of statements
3. elif expression 2:
4. # block of statements
5. elif expression 3:
6. # block of statements
7. else:
SRINIVASARAO G (PROF)-VITAP 26
PROBLEM SOLVING USING PYTHON
8. # block of statements
Example 1
1. number = int(input("Enter the number?"))
2. if number==10:
3. print("number is equals to 10")
4. elif number==50:
5. print("number is equal to 50");
6. elif number==100:
7. print("number is equal to 100");
8. else:
9. print("number is not equal to 10, 50 or 100");
Output:
SRINIVASARAO G (PROF)-VITAP 27
PROBLEM SOLVING USING PYTHON
Example 2
1. marks = int(input("Enter the marks? "))
9. else:
10. print("Sorry you are fail?")
SRINIVASARAO G (PROF)-VITAP 28
PROBLEM SOLVING USING PYTHON
Arithmetic Operators
Addition operator +
x = 10
y = 40
print(x + y)
# Output
50
name = "Kelly"
surname = "Ault"
print(surname + " " + name)
# Output
Ault Kelly
Subtraction -
x = 10
y = 40
print(y - x)
# Output
30
SRINIVASARAO G (PROF)-VITAP 29
PROBLEM SOLVING USING PYTHON
Multiplication *
x=2
y=4
z=5
print(x * y)
print(x * y * z)
# Output
8
40
name = "Jessa"
print(name * 3)
# Output
JessaJessaJessa
Division /
x=2
y=4
z=8
print(y / x)
print(z / y / x)
# Output
2.0
1.0
Floor division //
x=2
SRINIVASARAO G (PROF)-VITAP 30
PROBLEM SOLVING USING PYTHON
y=4
z = 2.2
# normal division
print(y / x)
# Output
2.0
print(y // x)
# Output
2
Modulus %
x = 15
y=4
print(x % y)
# Output
3
Exponent **
num = 2
# 2*2
print(num ** 2)
# 2*2*2
print(num ** 3)
SRINIVASARAO G (PROF)-VITAP 31
PROBLEM SOLVING USING PYTHON
# Output
4
8
x = 10
y=5
z=2
# Equal to
print(x == y) # False
# != Not Equal to
print(x != y) # True
print(10 != x) # False
SRINIVASARAO G (PROF)-VITAP 32
PROBLEM SOLVING USING PYTHON
Output
True
True
False
True
False
True
True
False
True
True
False
True
Assignment operators
a=4
b=2
a += b
print(a)
a=4
a -= 2
print(a)
a=4
a *= 2
SRINIVASARAO G (PROF)-VITAP 33
PROBLEM SOLVING USING PYTHON
print(a)
a=4
a /= 2
print(a)
a=4
a **= 2
print(a)
a=5
a %= 2
print(a)
a=4
a //= 2
print(a)
Output
6
2
8
2.0
16
1
2
SRINIVASARAO G (PROF)-VITAP 34
PROBLEM SOLVING USING PYTHON
Logical operators
b=4
# Logical and
else:
print("Do nothing")
Output
False
True
False
False
8
In the case of arithmetic values, Logical and always returns the second value; as a result,
see the following example.
Example
print(10 and 20) # 20
print(10 and 5) # 5
print(100 and 300) # 300
SRINIVASARAO G (PROF)-VITAP 35
PROBLEM SOLVING USING PYTHON
or (Logical or)
Example
print(True or False) # True
b=4
# Logical and
if a > 0 or b < 0:
# atleast one expression is true so conditions is true
print(a + b) # 6
else:
print("Do nothing")
Output
True
True
False
True
6
In the case of arithmetic values, Logical or it always returns the first value; as a result,
see the following code.
Example
print(10 or 20) # 10
SRINIVASARAO G (PROF)-VITAP 36
PROBLEM SOLVING USING PYTHON
print(10 or 5) # 10
print(100 or 300) # 100
Example
print(not False) # True return complements result
a = True
# Logical not
if not a:
# a is True so expression is False
print(a)
else:
print("Do nothing")
Output
Do nothing
In the case of arithmetic values, Logical not always return False for nonzero value.
Example
print(not 10) # False. Non-zero value
SRINIVASARAO G (PROF)-VITAP 37
PROBLEM SOLVING USING PYTHON
Membership operators
In operator
my_list = [11, 15, 21, 29, 50, 70]
number = 15
if number in my_list:
print("number is present")
else:
print("number is not present")
Output
number is present
Not in operator
Example
my_tuple = (11, 15, 21, 29, 50, 70)
number = 35
if number not in my_tuple:
print("number is not present")
else:
print("number is present")
Output
number not is present
SRINIVASARAO G (PROF)-VITAP 38
PROBLEM SOLVING USING PYTHON
Identity operators
is operator
x = 10
y = 11
z = 10
print(x is y) # it compare memory address of x andy
True
is not operator
x = 10
y = 11
z = 10
print(x is not y) # it campare memory address of x andy
Output
True
False
SRINIVASARAO G (PROF)-VITAP 39
PROBLEM SOLVING USING PYTHON
Bitwise Operators
b=4
c=5
print(a & b)
print(a & c)
print(b & c)
Output
4
5
4
Bitwise or |
a=7
b=4
c=5
print(a | b)
print(a | c)
print(b | c)
Output
7
7
5
SRINIVASARAO G (PROF)-VITAP 40
PROBLEM SOLVING USING PYTHON
Bitwise xor ^
a=7
b=4
c=5
print(a ^ c)
print(b ^ c)
Output
3
2
1
b=4
c=3
print(~a, ~b, ~c)
# Output -8 -5 -4
print(5 << 3)
# Output 40
print(4 >> 2)
# Output 1
print(5 >> 2)
# Output 1
SRINIVASARAO G (PROF)-VITAP 41
PROBLEM SOLVING USING PYTHON
In Python, operator precedence and associativity play an essential role in solving the expression.
An expression is the combination of variables and operators that evaluate based on operator
precedence.
We must know what the precedence (priority) of that operator is and how they will evaluate down
to a single value. Operator precedence is used in an expression to determine which operation to
perform first.
Example: -
print((10 - 4) * 2 +(10+2))
# Output 24
In the above example. 1st precedence goes to a parenthesis(), then for plus and minus
operators. The expression will be executed as.
(10 - 4) * 2 +(10+2)
6 * 2 + 12
12 + 12
1 (Highest) () Parenthesis
2 **
Exponent
5 + ,- Addition, Subtraction
SRINIVASARAO G (PROF)-VITAP 42
PROBLEM SOLVING USING PYTHON
SRINIVASARAO G (PROF)-VITAP 42
PROBLEM SOLVING USING PYTHON
8 ^
Bitwise XOR
9 | Bitwise OR
14 (Lowest) or Logical OR
SRINIVASARAO G (PROF)-VITAP 43