SlideShare a Scribd company logo
By,
Ms. Swapnali S. Gawali,
Asst. Prof. Computer Engineering
Sanjivani College of Engineering, Kopargaon
Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423 603
Department of Computer Engineering
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 2
Problem Solving in everyday life
People make decisions every day to solve problems that
affect their lives
• Important problems
• Unimportant problem
• Bad/Good decision
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 3
 There are six steps to follow to ensure the best decision:
1.Identify the problem.
2.Understand the problem
3.Identify alternative ways to solve the problem
4.Select the best way to solve the problem from the list of alternative
solutions
5.List instructions that enable you to solve the problem using the
selected solution
6.Evaluate the solution
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 4
 The first step toward solving a problem is to identify the problem
 What is the specific problem? (This means you should determine
what is that you want to change)
 Clearly define the goal that you want to achieve. (What are you
trying to achieve?)
 Determine what are the inputs and outputs
 If you don’t know what the problem is, you cannot solve it.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 5
 You must understand what is involved in the problem before you can
continue toward the solution
 This includes understanding the knowledge base of the person or
machine for whom you are solving the problem
 Also, you also must know your own knowledge base., You cannot
solve a problem if you do not know the subject. For example, to solve
a problem involving accounting, you must know accounting
 You can’t automatically desired the information
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 6
 Generate as many potential solutions as possible
 List the features for each possible solution
 You might want to talk to other people to find other solutions than
those you have identified.
 Alternative solutions must be acceptable ones
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 7
 In this step, you need to identify and evaluate the pros and cons of
each possible solution before selecting the best one
 In order to do this, you need to select criteria for the evaluation
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 8
 These numbered, step-by-step instructions must fall within the
knowledge base set up in step 2
 Do Stage
 Planning: Create a numbered, step-by-step set of instructions
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 9
 To evaluate or test a solution means to check its result to see if it is
correct, and to see if it satisfies the needs of the person(s) with the
problem. • Test the solution
 Are the results accurate?
 Does the solution solve the original problem?
 Does it satisfy the needs of the user?
 Is it acceptable to the user?
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 10
 The tasks required to solve the problem with Computer, are group into tree phases
 Identify the purpose
 Identifying parameters and constraints
 Collecting information
 Developing a program
 Identifying Logical Structure
 Writing Algorithm
 Drawing Flowchart
 Writing pseudocode
 Writing a computer program
 Debugging the program
 Executing a program
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 11
 Python is a popular high-level, interpreted programming language used in various
applications
 Python is an easy language to learn because of its simple syntax
 Python can be used for simple tasks such as plotting or for more complex tasks like
machine learning
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 12
 Created in 1990 by Guido van Rossum
 Named after Monty Python
 First public release in 1991
 comp.lang.python founded in 1994
 Open source from the start
 Considered a scripting language, but is much more
 Scalable, object oriented and functional from the beginning
 Used by Google from the beginning
 Increasingly popular
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 13
“Python is an experiment in how
much freedom program-mers need.
Too much freedom and nobody can
read another's code; too little and
expressive-ness is endangered.”
- Guido van Rossum
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 14
Features of Python
1. Free and
Open Source
2. Easy to
code
3. Easy to
Read 4. Object-Oriented
Language
5. GUI Programming
Support
6. High-Level
Language
7. Portable
8. Easy to
Debug
9. Integrated and
Interpreted
10. Large
Standard Library
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 15
 A value is one of the basic things a program work with, like letter or
a number.
 Example:
 2 – Integer
 42.0 – Floating Point
 ‘Hello’ – String
 We use variable to stored value.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 16
 A data type defines the type of data stored in a variable.
 Python is a dynamically typed language, so we do not need to defined
data types while declaring.
 Data types are the classification or categorization of data items.
Data Type
Numeric
Integer Complex
Number
Float
Dictionary Boolean Set Sequence
Strings Tuple
List
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 17
 It represent data which has numeric value.
 Three Categories:
 Integer: int
 Float: float
 Complex Number: complex
 Integer: It contains negative and positive whole numbers. (without
decimal or fraction) Example:101,-319,20
 Float: It is real number with floating-point. Example: 1.5,4.5,3.5
 Complex Number: It is specified as (real part)+(imaginary part)j
 example: -2+6j
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 18
 It is the ordered collection of similar or different data types.
 Sequences used to stored multiple values in an organized and
efficient fashion.
 Three Categories:
 String: ' '," ",''' '''
 List: [ ]
 Tuple: ()
 String: It is a collection of one or more characters put in ‘single
quote’, “double quote” and ‘‘‘ Triple quote”’
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 19
 List: It is like a array, which is ordered collection of data.
 It contains data of different types.
 The items are separated by comma(,) and enclosed with in square
bracket[].
 Example:A
list = [1,2,3,4,5,6]
fruit=["Apple", "mango", "banana"]
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 20
 Tuple: It is Like list, it also contains data of different types. The
items are separated by comma(,) and enclosed with in square
bracket().
 Tuple is immutable.
 Example:
fruit=("Apple", "mango", "banana")
tuple= (1,2,3,4,5)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 21
 It provides two built in values, True and False.
 It determine the given value is true or false.
 It is denoted by class ' bool '.
 It also represented by ' 0 ' or ' 1 '
Example:
print(11>9)
a=200, b=33
print(a<b)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 22
 It is unordered collection of the data type.
 It is iterable, mutable and has unique elements.
 In it order of element is undefined.
 It is created by using built-in set() function, or a sequence of
elements is passed in curly braces and separated by comma.
 Example:
my_set = {1,2,3}
myset = {1.0, ' Hi ', (1,2,3,4)}
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 23
 It is an unordered set of a key-value pair of items.
 It is like an associative array or hash table where each key stores a
specific value.
 Key can store primitive data types.
 Value is an arbitrary Python Object.
 Example:
thisdict = {' brand ' : ' Maruti ', ' model ' : ' Swift ', ' year ' : 2000}
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 24
 Variables are containers for storing data values.
 Unlike other programming languages, Python has no command for declaring a
variable.
 A variable is created the moment we first assign a value to it.
 x = 5
y = "John"
print(x) 5
print(y) John
 Variables do not need to be declared with any particular type and can even change
type after they have been set.
 String variables can be declared either by using single or double quotes: ‘John’
or”John”
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 25
 Following are rules for creating variables in Python
1. A variable name must start with a letter or the underscore character.
 example: a, name, _name_
2. A variable name cannot start with a number.
 Example: This is not allowed: 12a
3. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ ).
 Example: Name, a12
4. Variable names are case-sensitive (name, Name and NAME are three different
variables).
 Example: This A is different than a
5. The reserved words(keywords) cannot be used naming the variable.
 Example: This word not allowed
and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from,
global, if, import, in, is, not, or, pass, print, raise, return, try, while
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 26
 Input is any information provided to the program
– Keyboard input
– Mouse input
– File input
– Sensor
input (microphone, camera, photo cell, etc.)
 Output is any information (or effect) that a program produces:
– sounds, lights, pictures, text, motion, etc.
Output( on a screen, in a file, on a disk or tape, etc.)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 27
 print(): Used to print output.
 Example 1:
print ('Hello World’)
print ("Sanjivani")
print('''College of Engineering''')
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 28
Output
Hello World
Sanjivani
College of Engineering
 Example 2:
name='Sonal’
rollno=10
print('Name is ',name)
print('Roll no is ',rollno)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 29
Output
Name is Sonal
Roll no is 10
Example 3:
name='Sonal’
rollno=10
print('Name is ',name, 'Roll no is
',rollno)
Output
Name is Sonal Roll no is 10
 Example 4:
name='Sonal’
rollno=10
print(f'Name is {name}. Roll no is {rollno}.')
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 30
Output
Name is Sonal. Roll no is 10.
 Syntax of print()
print(object(s), sep=separator, end=end, file=file, flush=flush)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 31
Parameter Description
object(s) Any object, and as many as you like. Will be converted to string before
printed
sep='separator' Optional. Specify how to separate the objects, if there is more than one.
Default is ' '
end='end' Optional. Specify what to print at the end. Default is 'n' (line feed)
file Optional. An object with a write method. Default is sys.stdout
flush Optional. A Boolean, specifying if the output is flushed (True) or buffered
(False). Default is False
 sep parameter: is used to specify the separator between the strings.
 Example:
name='Sonal’
rollno=10
print(f'Name is {name}',f' Roll no is {rollno}',sep='.')
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 32
Output
Name is Sonal. Roll no is 10
 end parameter: . Specify what to print at the end.
 Example:
name='Sonal’
rollno=10
print(f'Name is {name}.',end=' ‘)
print(f' Roll no is {rollno}.')
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 33
Output
Name is Sonal. Roll no is 10.
 Python input() function is used to take user input.
 By default, it returns the user input in form of a string.
 Syntax:
input('prompt message’)
 Example:
name=input('Enter your name’)
print(f'Name is {name}.’)
print(type(name))
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 34
Output
Enter your nameSonal
Name is Sonal.<class 'str'>
 Example:
name=input('Enter your name’)
print(f'Name is {name}.’)
print(type(name))
rollno=int(input('Enter roll no’))
#int() use to convert string to integer value
print(f'Roll no is {rollno}.’)
print(type(rollno))
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 35
Output
Enter your nameSonal
Name is Sonal.
<class 'str’>
Enter roll no10
Roll no is 10.
<class 'int'>
 Operators:Operators are symbol used to perform operations on
variables and values.
 Operands:
The values or variables that an operator acts on are called
operands.
 A sequence of operands and operators is called expression.
Example: a+b-5
A + B - 5
Operands
Operator
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 36
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Identity operators
6. Membership operators
7. Bitwise operators
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 37
1. Arithmetic operators are used with numeric values to perform
common mathematical operations:
2. Assume x=10, y=5
Operator Name Example Output
+ Addition print(x+y) 15
- Subtraction print(x-y) 5
* Multiplication print(x*y) 50
/ Division print(x/y) 2.0
% Modulus print(x%y) 0
** Exponentiation print(x**y) 100000
// Floor Division x//y 2
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 38
a=10
b=20
c=a+b
print(“Addition is ”,c)
c=a-b
print(“Subtraction is ”,c)
c=a*b
print(“Multiplication is ”,c)
c=a/b
print(“Division is ”,c)
Output:
Addition is 30
Subtraction is -10
Multiplication is 200
Division is 0.5
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 39
 Assignment operators are used to assign values to variables:
Operator Description
= Assign value of right side of expression to left side operand
+= Add and Assign: Add right side operand with left side operand and then assign to left operand
-= Subtract AND: Subtract right operand from left operand and then assign to left operand: True if both operands
are equal
*= Multiply AND: Multiply right operand with left operand and then assign to left operand
/= Divide AND: Divide left operand with right operand and then assign to left operand
%= Modulus AND: Takes modulus using left and right operands and assign result to left operand
//= Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand
**= Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 40
a=10
b=20
print("a is ",a)
print("b is ",b)
a += 1
print("Add 1 to a ",a)
b -= 1
print("Subtract 1 from b ",b)
a **= 2
print("a square is ", a)
Output:
a is 10
b is 20
Add 1 to a 11
Subtract 1 from b 19
a square is 121
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 41
 Comparison operators are used to compare two values:
Operator Name Example
== Equal a==b
!= Not Equal a!=b
> Greater than a>b
< Less than a<b
>= Greater than or equal to a>=b
<= Less than or equal to a<=b
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 42
x=10
y=20
print(x==y)
print(x!=y)
print(x<y)
print(x>y)
print(x<=y)
print(x>=y)
Output:
False
True
True
False
True
False
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 43
 Logical operators are used to combine conditional statements:
Operator Description Example
and Returns True if both statements
are true
x<5 and x<10
or Returns True if one of the
statements is true
x<5 or x<4
not Reverse the result, returns False
if the result is true
not(x<5 and x<10)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 44
x=10
y=20
print(x==y and x<y)
print(x!=y or x>y)
print(not(x<y))
Output:
False
True
False
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 45
 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:
Operator Description Example
is Returns true if both
variables are the same object
a is y
is not Returns true if both
variables are not the same
object
a is not y
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 46
x=10
y=10
print(x is y)
print(x is not y)
Output:
True
False
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 47
 Membership operators are used to test if a sequence is presented in an
object:
Operator Description Example
in Returns True if a sequence with the
specified value is present in the
object
a in b
not in Returns True if a sequence with the
specified value is not present in the
object
a not in b
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 48
x='sanjivani’
print('s' in x)
print('s' not in x)
print('S' in x)
print('S' not in x)
y='anji’
print(y in x)
print(x in y)
Output:
True
False
False
True
True
False
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 49
 In Python, bitwise operators are used to performing bitwise calculations on integers.
 The integers are first converted into binary and then operations are performed on bit
by bit, hence the name bitwise operators.
 Then the result is returned in decimal format.
OPERATOR DESCRIPTION SYNTAX
& Bitwise AND x & y
| Bitwise OR x | y
~ Bitwise NOT ~x
^ Bitwise XOR x ^ y
>> Bitwise right shift x>>
<< Bitwise left shift x<<
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 50
 Bitwise AND operator Returns 1 if both the bits are 1 else 0.
 Example:
a=10= 1010 (Binary)
b=4=0100 (Binary)
a&b = 1010
&
0100
=0000(Binary)
=0 (Decimal)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 51
 Bitwise or operator Returns 1 if either of the bit is 1 else 0.
 Example:
a=10= 1010 (Binary)
b=4=0100 (Binary)
A|b = 1010
|
0100
=1110(Binary)
=14 (Decimal)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 52
 Bitwise not operator: Returns one’s complement of the number.
 Example:
a=10=1010 (Binary)
~a=~1010
=~(1010)
= (0101)
= 5 (Decimal)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 53
 Bitwise xor operator: Returns 1 if one of the bits is 1 and the other is 0 else
returns false.
 Example:
a=10= 1010 (Binary)
b=4=0100 (Binary)
a^b = 1010
^
0100
=1110(Binary)
=14 (Decimal)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 54
a = 10
b = 4
print("a & b =", a & b)
print("a | b =", a | b)
print("a ^ b =", a ^ b)
Output:
a & b = 0
a | b = 14
a ^ b = 14
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 55
 Bitwise right shift: Shifts the bits of the number to the right and fills 0 on voids
left( fills 1 in the case of a negative number) as a result.
 Similar effect as of dividing the number with some power of two.
 Example:
a=10= 0000 1010 (Binary)
a>>1=0000 0101 = 5
Example 2:
a= -10 = 1111 0110
a>>1 = 1111 1011 = -5
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 56
 Bitwise left shift: Shifts the bits of the number to the left and fills 0 on voids right
as a result.
 Similar effect as of multiplying the number with some power of two.
 Example:
a=10= 0000 1010 (Binary)
A<<1= 0001 0100 = 20
Example 2:
a= -10 = 1111 0110
A<<1 = 1110 1100 = -20
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 57
a = 10
b = -10
print("a >> 1 =", a >> 1)
print("b >> 1 =", b >> 1)
a = 5
b = -10
print("a << 1 =", a << 1)
print("b << 1 =", b << 1)
Output:
a >> 1 = 5
b >> 1 = -5
a << 1 = 10
b << 1 = -20
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 58
 A statement is an instruction that the Python interpreter can
execute.
 Python statement ends with the token NEWLINE character. It
means each line in a Python script is a statement.
 There are mainly four types of statements in Python,
 print statements: print(“hello”)
 Assignment statements: a=100
 Conditional statements: if (a>10):
Print(“a is greater than 10”)
 Looping statements.: for x in range(6):
print(x)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 59
Python statement ends with the token NEWLINE character.
But we can extend the statement over multiple lines using line
continuation character ().
This is known as an explicit continuation.
add=20+30+
50+60+
90
print(add)
Output: 250
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 60
We can use parentheses () to write a multi-line statement.
We can add a line continuation statement inside it.
Whatever we add inside a parentheses () will treat as a single
statement even it is placed on multiple lines.
addition = (10 + 20 +
30 + 40 +
50 + 60 + 70)
print(addition)
# Output: 280
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 61
 Compound statement contain (groups of) other statements; they affect or control the
execution of those other statements in some way.
 The compound statement includes the conditional and loop statement.
 if statement: t is a control flow statement that will execute statements under it if
the condition is true. Also known as a conditional statement.
 while statement: The while loop statement repeatedly executes a code block while a
particular condition is true. Also known as a looping statement.
 for statement: Using for loop statement, we can iterate any sequence or iterable
variable. The sequence can be string, list, dictionary, set, or tuple. Also known as a
looping statement.
 try statement: specifies exception handlers.
 with statement: Used to cleanup code for a group of statements, while the with
statement allows the execution of initialization and finalization code around a
block of code.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 62
 Python has various simple statements for a specific purpose.
1. Expression Statement
2. The pass statement
3. The del statement
4. The return statement
5. The import statement
6. The continue and break statement
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 63
 A combination of operands and operators is called an expression.
 The expression in Python produces some value or result after being
interpreted by the Python interpreter.
 Example:
r = a+b
res =10+a
result = 20+30
 An expression in Python can contain identifiers, operators, and operands.
 An identifier is a name that is used to define and identify a class, variable,
or function in Python.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 64
1.Constant Expressions
2.Arithmetic Expressions
3.Integral Expressions
4.Floating Expressions
5.Relational Expressions
6.Logical Expressions
7.Combinational Expression
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 65
 A constant expression in Python that contains only constant values
is known as a constant expression.
 In a constant expression in Python, the operator(s) is a constant.
 A constant is a value that cannot be changed after its initialization.
 Example:
a = 20 +10
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 66
 An expression in Python that contains a combination of arithmetic
operators, operands, and sometimes parenthesis is known as
an arithmetic expression.
 The result of an arithmetic expression is also a numeric value
 Example:
x = c+d
x = c * a
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 67
 An integral expression in Python is used for computations and type
conversion (integer to float, a string to integer, etc.).
 An integral expression always produces an integer value as a
resultant.
 Example:
a = 5
b = 5.0
res = a + int(b)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 68
 A floating expression in Python is used for computations and type
conversion (integer to float, a string to integer, etc.).
 A floating expression always produces a floating-point number as a
resultant.
 Example:
a = 5
b = 5.0
res = float(a) + b
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 69
 A relational expression in Python can be considered as a
combination of two or more arithmetic expressions joined using
relational operators.
 The overall expression results in either True or False (boolean
result).
 We have four types of relational operators in Python (i.e. > , < , >= ,
<=)(i.e.>,<,>=,<=).
 A relational operator produces a boolean result so they are also
known as Boolean Expressions.
 Example:
10+15>20
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 70
 As the name suggests, a logical expression performs the logical
computation using logical operators, and the overall expression
results in either True or False (boolean result).
 Example:
a and b
a or b
a not b
a=3
b=3
if a>0 and b>0:
print(“a and b greater than zero”)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 71
 As the name suggests, a combination expression can contain a single
or multiple expressions which result in an integer or boolean value
depending upon the expressions involved.
 Example:
x = 5
y = 5
res = x + (2 *6)
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 72
 A boolean expression is an expression that is either true or false.
 Example:
5 > 3
True
5 >9
False
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 73
Python defines type conversion functions to directly
convert one data type to another which is useful in day-to-
day and competitive programming.
There are two types of Type Conversion in Python:
 Implicit Type Conversion
 Explicit Type Conversion
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 74
 In Implicit type conversion of data types in Python, the Python
interpreter automatically converts one data type to another without
any user involvement.
Program Output
x = 10
print("x is of type:",type(x))
y = 10.6
print("y is of type:",type(y))
z = x + y
print(z)
print("z is of type:",type(z))
x is of type: <class 'int'>
y is of type: <class 'float'>
20.6
z is of type: <class 'float'>
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 75
1.int(a, base): This function
converts any data type to integer.
‘Base’ specifies the base in which
string is if the data type is a string.
2.float(): This function is used to
convert any data type to a floating-
point number.
3.tuple() : This function is used
to convert to a tuple.
4.set() : This function returns the type
after converting to set.
5.list() : This function is used to
convert any data type to a list type.
6.dict() : This function is used
to convert a tuple of order (key,value)
into a dictionary.
7.str() : Used to convert integer into a
string.
8.complex(real,imag) : This
function converts real numbers to
complex(real,imag) number.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 76
In Explicit Type Conversion in Python, the data type is manually
changed by the user as per their requirement.
Various forms of explicit type conversion are explained below:
x=5
print("x is of type:",type(x))
y=float(x)
print("y is of type:",type(y))
z=str(x)
print("z is of type:",type(z))
Output:
x is of type: <class 'int’>
y is of type: <class ‘float’>
z is of type: <class ‘str'>
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 77
Solve this :
3 + 4 x 2 – 1
3 + 4 / 2 – 1
 BIDMAS
 BIDMAS, which stands
for Brackets, Indices, Division, Multiplication, Addition
and Subtraction.
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 78
 The operator precedence in Python is listed in the following table. It is
in descending order (upper group has higher precedence than the lower
ones).
Priority Operator Meaning
1 () Parentheses
2 ** Exponent
3 +,-,~ Unary plus, Unary Minus, Bitwise NOT
4 *,/,//,% Multiplication, Division, Floor division,
Modulus
5 +,- Addition, Subtraction
6 <<,>> Bitwise shift operators
7 & Bitwise AND
8 ^ Bitwise XOR
9 | Bitwise OR
10 ==, !=, >, >=, <, <=, is, is not, in, not
in
Comparisons, Identity, Membership operators
11 not Logical NOT
12 and Logical AND
13 or Logical OR
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 79
Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 80

More Related Content

PDF
ESIT135 Problem Solving Using Python Notes of Unit-1 and Unit-2
PPTX
Unit 1- Python- Features, Variables, Data Types, Operators and Expressions
PPTX
ITE Course Unit 1Productivity Tools For An Engineers
PPTX
ESIT135: Unit 3 Topic: functions in python
PPTX
list.pptx
PPTX
Unit4-Basic Concepts and methods of Dictionary.pptx
PPTX
Unit-4 Basic Concepts of Tuple in Python .pptx
PDF
Stack Applications : Infix to postfix conversion, Evaluation of postfix expre...
ESIT135 Problem Solving Using Python Notes of Unit-1 and Unit-2
Unit 1- Python- Features, Variables, Data Types, Operators and Expressions
ITE Course Unit 1Productivity Tools For An Engineers
ESIT135: Unit 3 Topic: functions in python
list.pptx
Unit4-Basic Concepts and methods of Dictionary.pptx
Unit-4 Basic Concepts of Tuple in Python .pptx
Stack Applications : Infix to postfix conversion, Evaluation of postfix expre...

What's hot (20)

PPTX
JAVASRIPT and PHP (Hypertext Preprocessor)
PDF
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
PDF
Sorting Algorithms: Bubble Sort, Selection Sort,
PDF
Insertion Sort, Merge Sort. Time complexity of all sorting algorithms and t...
PDF
Introduction to Stack, ▪ Stack ADT, ▪ Implementation of Stack using array, ...
PPTX
Python Datatypes.pptx
PPTX
List in python
PPTX
Tuple in python
PPTX
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PDF
Python tuples and Dictionary
PDF
9 python data structure-2
PDF
Set methods in python
PDF
Introduction to python programming
PDF
Learn 90% of Python in 90 Minutes
PDF
Python Programming
PPTX
19. Data Structures and Algorithm Complexity
PDF
Data handling CBSE PYTHON CLASS 11
PPTX
6-Python-Recursion PPT.pptx
JAVASRIPT and PHP (Hypertext Preprocessor)
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
Sorting Algorithms: Bubble Sort, Selection Sort,
Insertion Sort, Merge Sort. Time complexity of all sorting algorithms and t...
Introduction to Stack, ▪ Stack ADT, ▪ Implementation of Stack using array, ...
Python Datatypes.pptx
List in python
Tuple in python
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python tuples and Dictionary
9 python data structure-2
Set methods in python
Introduction to python programming
Learn 90% of Python in 90 Minutes
Python Programming
19. Data Structures and Algorithm Complexity
Data handling CBSE PYTHON CLASS 11
6-Python-Recursion PPT.pptx
Ad

Similar to ESIT135 : Unit 1 Python Basics Concepts (20)

PDF
Python-content-1.pdf
PDF
To get the notes of python of MCA batchs
PPTX
2024-25 TYBSC(CS)-PYTHON_PROG_ControlStructure.pptx
PDF
GE3151_PSPP_All unit _Notes
PDF
report on internshala python training
PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
PDF
computer science CLASS 11 AND 12 SYLLABUS.pdf
PDF
First Steps in Python Programming
PPTX
PYTHON PPT.pptx python is very useful for day to day life
PPT
python language programming presentation
PPTX
An Introduction : Python
PDF
MIT6_0001F16_Lec1.pdf
PPTX
python presntation 2.pptx
PPTX
Python ppt_118.pptx
PDF
Introduction to Python Programming | InsideAIML
PPTX
Introduction to Python Programming Language
PDF
Python: An introduction A summer workshop
PPTX
Python Conditional and Looping statements.pptx
PPTX
Python introduction towards data science
PDF
Python Programming by Dr. C. Sreedhar.pdf
Python-content-1.pdf
To get the notes of python of MCA batchs
2024-25 TYBSC(CS)-PYTHON_PROG_ControlStructure.pptx
GE3151_PSPP_All unit _Notes
report on internshala python training
Dr.C S Prasanth-Physics ppt.pptx computer
computer science CLASS 11 AND 12 SYLLABUS.pdf
First Steps in Python Programming
PYTHON PPT.pptx python is very useful for day to day life
python language programming presentation
An Introduction : Python
MIT6_0001F16_Lec1.pdf
python presntation 2.pptx
Python ppt_118.pptx
Introduction to Python Programming | InsideAIML
Introduction to Python Programming Language
Python: An introduction A summer workshop
Python Conditional and Looping statements.pptx
Python introduction towards data science
Python Programming by Dr. C. Sreedhar.pdf
Ad

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
RMMM.pdf make it easy to upload and study
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Computing-Curriculum for Schools in Ghana
PDF
Trump Administration's workforce development strategy
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharma ospi slides which help in ospi learning
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana
Trump Administration's workforce development strategy
Orientation - ARALprogram of Deped to the Parents.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Structure & Organelles in detailed.
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx

ESIT135 : Unit 1 Python Basics Concepts

  • 1. By, Ms. Swapnali S. Gawali, Asst. Prof. Computer Engineering Sanjivani College of Engineering, Kopargaon Sanjivani Rural Education Society’s Sanjivani College of Engineering, Kopargaon-423 603 Department of Computer Engineering
  • 2. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 2
  • 3. Problem Solving in everyday life People make decisions every day to solve problems that affect their lives • Important problems • Unimportant problem • Bad/Good decision Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 3
  • 4.  There are six steps to follow to ensure the best decision: 1.Identify the problem. 2.Understand the problem 3.Identify alternative ways to solve the problem 4.Select the best way to solve the problem from the list of alternative solutions 5.List instructions that enable you to solve the problem using the selected solution 6.Evaluate the solution Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 4
  • 5.  The first step toward solving a problem is to identify the problem  What is the specific problem? (This means you should determine what is that you want to change)  Clearly define the goal that you want to achieve. (What are you trying to achieve?)  Determine what are the inputs and outputs  If you don’t know what the problem is, you cannot solve it. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 5
  • 6.  You must understand what is involved in the problem before you can continue toward the solution  This includes understanding the knowledge base of the person or machine for whom you are solving the problem  Also, you also must know your own knowledge base., You cannot solve a problem if you do not know the subject. For example, to solve a problem involving accounting, you must know accounting  You can’t automatically desired the information Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 6
  • 7.  Generate as many potential solutions as possible  List the features for each possible solution  You might want to talk to other people to find other solutions than those you have identified.  Alternative solutions must be acceptable ones Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 7
  • 8.  In this step, you need to identify and evaluate the pros and cons of each possible solution before selecting the best one  In order to do this, you need to select criteria for the evaluation Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 8
  • 9.  These numbered, step-by-step instructions must fall within the knowledge base set up in step 2  Do Stage  Planning: Create a numbered, step-by-step set of instructions Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 9
  • 10.  To evaluate or test a solution means to check its result to see if it is correct, and to see if it satisfies the needs of the person(s) with the problem. • Test the solution  Are the results accurate?  Does the solution solve the original problem?  Does it satisfy the needs of the user?  Is it acceptable to the user? Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 10
  • 11.  The tasks required to solve the problem with Computer, are group into tree phases  Identify the purpose  Identifying parameters and constraints  Collecting information  Developing a program  Identifying Logical Structure  Writing Algorithm  Drawing Flowchart  Writing pseudocode  Writing a computer program  Debugging the program  Executing a program Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 11
  • 12.  Python is a popular high-level, interpreted programming language used in various applications  Python is an easy language to learn because of its simple syntax  Python can be used for simple tasks such as plotting or for more complex tasks like machine learning Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 12
  • 13.  Created in 1990 by Guido van Rossum  Named after Monty Python  First public release in 1991  comp.lang.python founded in 1994  Open source from the start  Considered a scripting language, but is much more  Scalable, object oriented and functional from the beginning  Used by Google from the beginning  Increasingly popular Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 13
  • 14. “Python is an experiment in how much freedom program-mers need. Too much freedom and nobody can read another's code; too little and expressive-ness is endangered.” - Guido van Rossum Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 14
  • 15. Features of Python 1. Free and Open Source 2. Easy to code 3. Easy to Read 4. Object-Oriented Language 5. GUI Programming Support 6. High-Level Language 7. Portable 8. Easy to Debug 9. Integrated and Interpreted 10. Large Standard Library Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 15
  • 16.  A value is one of the basic things a program work with, like letter or a number.  Example:  2 – Integer  42.0 – Floating Point  ‘Hello’ – String  We use variable to stored value. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 16
  • 17.  A data type defines the type of data stored in a variable.  Python is a dynamically typed language, so we do not need to defined data types while declaring.  Data types are the classification or categorization of data items. Data Type Numeric Integer Complex Number Float Dictionary Boolean Set Sequence Strings Tuple List Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 17
  • 18.  It represent data which has numeric value.  Three Categories:  Integer: int  Float: float  Complex Number: complex  Integer: It contains negative and positive whole numbers. (without decimal or fraction) Example:101,-319,20  Float: It is real number with floating-point. Example: 1.5,4.5,3.5  Complex Number: It is specified as (real part)+(imaginary part)j  example: -2+6j Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 18
  • 19.  It is the ordered collection of similar or different data types.  Sequences used to stored multiple values in an organized and efficient fashion.  Three Categories:  String: ' '," ",''' '''  List: [ ]  Tuple: ()  String: It is a collection of one or more characters put in ‘single quote’, “double quote” and ‘‘‘ Triple quote”’ Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 19
  • 20.  List: It is like a array, which is ordered collection of data.  It contains data of different types.  The items are separated by comma(,) and enclosed with in square bracket[].  Example:A list = [1,2,3,4,5,6] fruit=["Apple", "mango", "banana"] Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 20
  • 21.  Tuple: It is Like list, it also contains data of different types. The items are separated by comma(,) and enclosed with in square bracket().  Tuple is immutable.  Example: fruit=("Apple", "mango", "banana") tuple= (1,2,3,4,5) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 21
  • 22.  It provides two built in values, True and False.  It determine the given value is true or false.  It is denoted by class ' bool '.  It also represented by ' 0 ' or ' 1 ' Example: print(11>9) a=200, b=33 print(a<b) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 22
  • 23.  It is unordered collection of the data type.  It is iterable, mutable and has unique elements.  In it order of element is undefined.  It is created by using built-in set() function, or a sequence of elements is passed in curly braces and separated by comma.  Example: my_set = {1,2,3} myset = {1.0, ' Hi ', (1,2,3,4)} Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 23
  • 24.  It is an unordered set of a key-value pair of items.  It is like an associative array or hash table where each key stores a specific value.  Key can store primitive data types.  Value is an arbitrary Python Object.  Example: thisdict = {' brand ' : ' Maruti ', ' model ' : ' Swift ', ' year ' : 2000} Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 24
  • 25.  Variables are containers for storing data values.  Unlike other programming languages, Python has no command for declaring a variable.  A variable is created the moment we first assign a value to it.  x = 5 y = "John" print(x) 5 print(y) John  Variables do not need to be declared with any particular type and can even change type after they have been set.  String variables can be declared either by using single or double quotes: ‘John’ or”John” Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 25
  • 26.  Following are rules for creating variables in Python 1. A variable name must start with a letter or the underscore character.  example: a, name, _name_ 2. A variable name cannot start with a number.  Example: This is not allowed: 12a 3. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).  Example: Name, a12 4. Variable names are case-sensitive (name, Name and NAME are three different variables).  Example: This A is different than a 5. The reserved words(keywords) cannot be used naming the variable.  Example: This word not allowed and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, not, or, pass, print, raise, return, try, while Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 26
  • 27.  Input is any information provided to the program – Keyboard input – Mouse input – File input – Sensor input (microphone, camera, photo cell, etc.)  Output is any information (or effect) that a program produces: – sounds, lights, pictures, text, motion, etc. Output( on a screen, in a file, on a disk or tape, etc.) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 27
  • 28.  print(): Used to print output.  Example 1: print ('Hello World’) print ("Sanjivani") print('''College of Engineering''') Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 28 Output Hello World Sanjivani College of Engineering
  • 29.  Example 2: name='Sonal’ rollno=10 print('Name is ',name) print('Roll no is ',rollno) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 29 Output Name is Sonal Roll no is 10 Example 3: name='Sonal’ rollno=10 print('Name is ',name, 'Roll no is ',rollno) Output Name is Sonal Roll no is 10
  • 30.  Example 4: name='Sonal’ rollno=10 print(f'Name is {name}. Roll no is {rollno}.') Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 30 Output Name is Sonal. Roll no is 10.
  • 31.  Syntax of print() print(object(s), sep=separator, end=end, file=file, flush=flush) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 31 Parameter Description object(s) Any object, and as many as you like. Will be converted to string before printed sep='separator' Optional. Specify how to separate the objects, if there is more than one. Default is ' ' end='end' Optional. Specify what to print at the end. Default is 'n' (line feed) file Optional. An object with a write method. Default is sys.stdout flush Optional. A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False
  • 32.  sep parameter: is used to specify the separator between the strings.  Example: name='Sonal’ rollno=10 print(f'Name is {name}',f' Roll no is {rollno}',sep='.') Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 32 Output Name is Sonal. Roll no is 10
  • 33.  end parameter: . Specify what to print at the end.  Example: name='Sonal’ rollno=10 print(f'Name is {name}.',end=' ‘) print(f' Roll no is {rollno}.') Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 33 Output Name is Sonal. Roll no is 10.
  • 34.  Python input() function is used to take user input.  By default, it returns the user input in form of a string.  Syntax: input('prompt message’)  Example: name=input('Enter your name’) print(f'Name is {name}.’) print(type(name)) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 34 Output Enter your nameSonal Name is Sonal.<class 'str'>
  • 35.  Example: name=input('Enter your name’) print(f'Name is {name}.’) print(type(name)) rollno=int(input('Enter roll no’)) #int() use to convert string to integer value print(f'Roll no is {rollno}.’) print(type(rollno)) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 35 Output Enter your nameSonal Name is Sonal. <class 'str’> Enter roll no10 Roll no is 10. <class 'int'>
  • 36.  Operators:Operators are symbol used to perform operations on variables and values.  Operands: The values or variables that an operator acts on are called operands.  A sequence of operands and operators is called expression. Example: a+b-5 A + B - 5 Operands Operator Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 36
  • 37. 1. Arithmetic operators 2. Assignment operators 3. Comparison operators 4. Logical operators 5. Identity operators 6. Membership operators 7. Bitwise operators Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 37
  • 38. 1. Arithmetic operators are used with numeric values to perform common mathematical operations: 2. Assume x=10, y=5 Operator Name Example Output + Addition print(x+y) 15 - Subtraction print(x-y) 5 * Multiplication print(x*y) 50 / Division print(x/y) 2.0 % Modulus print(x%y) 0 ** Exponentiation print(x**y) 100000 // Floor Division x//y 2 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 38
  • 39. a=10 b=20 c=a+b print(“Addition is ”,c) c=a-b print(“Subtraction is ”,c) c=a*b print(“Multiplication is ”,c) c=a/b print(“Division is ”,c) Output: Addition is 30 Subtraction is -10 Multiplication is 200 Division is 0.5 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 39
  • 40.  Assignment operators are used to assign values to variables: Operator Description = Assign value of right side of expression to left side operand += Add and Assign: Add right side operand with left side operand and then assign to left operand -= Subtract AND: Subtract right operand from left operand and then assign to left operand: True if both operands are equal *= Multiply AND: Multiply right operand with left operand and then assign to left operand /= Divide AND: Divide left operand with right operand and then assign to left operand %= Modulus AND: Takes modulus using left and right operands and assign result to left operand //= Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand **= Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 40
  • 41. a=10 b=20 print("a is ",a) print("b is ",b) a += 1 print("Add 1 to a ",a) b -= 1 print("Subtract 1 from b ",b) a **= 2 print("a square is ", a) Output: a is 10 b is 20 Add 1 to a 11 Subtract 1 from b 19 a square is 121 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 41
  • 42.  Comparison operators are used to compare two values: Operator Name Example == Equal a==b != Not Equal a!=b > Greater than a>b < Less than a<b >= Greater than or equal to a>=b <= Less than or equal to a<=b Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 42
  • 44.  Logical operators are used to combine conditional statements: Operator Description Example and Returns True if both statements are true x<5 and x<10 or Returns True if one of the statements is true x<5 or x<4 not Reverse the result, returns False if the result is true not(x<5 and x<10) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 44
  • 45. x=10 y=20 print(x==y and x<y) print(x!=y or x>y) print(not(x<y)) Output: False True False Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 45
  • 46.  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: Operator Description Example is Returns true if both variables are the same object a is y is not Returns true if both variables are not the same object a is not y Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 46
  • 47. x=10 y=10 print(x is y) print(x is not y) Output: True False Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 47
  • 48.  Membership operators are used to test if a sequence is presented in an object: Operator Description Example in Returns True if a sequence with the specified value is present in the object a in b not in Returns True if a sequence with the specified value is not present in the object a not in b Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 48
  • 49. x='sanjivani’ print('s' in x) print('s' not in x) print('S' in x) print('S' not in x) y='anji’ print(y in x) print(x in y) Output: True False False True True False Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 49
  • 50.  In Python, bitwise operators are used to performing bitwise calculations on integers.  The integers are first converted into binary and then operations are performed on bit by bit, hence the name bitwise operators.  Then the result is returned in decimal format. OPERATOR DESCRIPTION SYNTAX & Bitwise AND x & y | Bitwise OR x | y ~ Bitwise NOT ~x ^ Bitwise XOR x ^ y >> Bitwise right shift x>> << Bitwise left shift x<< Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 50
  • 51.  Bitwise AND operator Returns 1 if both the bits are 1 else 0.  Example: a=10= 1010 (Binary) b=4=0100 (Binary) a&b = 1010 & 0100 =0000(Binary) =0 (Decimal) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 51
  • 52.  Bitwise or operator Returns 1 if either of the bit is 1 else 0.  Example: a=10= 1010 (Binary) b=4=0100 (Binary) A|b = 1010 | 0100 =1110(Binary) =14 (Decimal) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 52
  • 53.  Bitwise not operator: Returns one’s complement of the number.  Example: a=10=1010 (Binary) ~a=~1010 =~(1010) = (0101) = 5 (Decimal) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 53
  • 54.  Bitwise xor operator: Returns 1 if one of the bits is 1 and the other is 0 else returns false.  Example: a=10= 1010 (Binary) b=4=0100 (Binary) a^b = 1010 ^ 0100 =1110(Binary) =14 (Decimal) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 54
  • 55. a = 10 b = 4 print("a & b =", a & b) print("a | b =", a | b) print("a ^ b =", a ^ b) Output: a & b = 0 a | b = 14 a ^ b = 14 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 55
  • 56.  Bitwise right shift: Shifts the bits of the number to the right and fills 0 on voids left( fills 1 in the case of a negative number) as a result.  Similar effect as of dividing the number with some power of two.  Example: a=10= 0000 1010 (Binary) a>>1=0000 0101 = 5 Example 2: a= -10 = 1111 0110 a>>1 = 1111 1011 = -5 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 56
  • 57.  Bitwise left shift: Shifts the bits of the number to the left and fills 0 on voids right as a result.  Similar effect as of multiplying the number with some power of two.  Example: a=10= 0000 1010 (Binary) A<<1= 0001 0100 = 20 Example 2: a= -10 = 1111 0110 A<<1 = 1110 1100 = -20 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 57
  • 58. a = 10 b = -10 print("a >> 1 =", a >> 1) print("b >> 1 =", b >> 1) a = 5 b = -10 print("a << 1 =", a << 1) print("b << 1 =", b << 1) Output: a >> 1 = 5 b >> 1 = -5 a << 1 = 10 b << 1 = -20 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 58
  • 59.  A statement is an instruction that the Python interpreter can execute.  Python statement ends with the token NEWLINE character. It means each line in a Python script is a statement.  There are mainly four types of statements in Python,  print statements: print(“hello”)  Assignment statements: a=100  Conditional statements: if (a>10): Print(“a is greater than 10”)  Looping statements.: for x in range(6): print(x) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 59
  • 60. Python statement ends with the token NEWLINE character. But we can extend the statement over multiple lines using line continuation character (). This is known as an explicit continuation. add=20+30+ 50+60+ 90 print(add) Output: 250 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 60
  • 61. We can use parentheses () to write a multi-line statement. We can add a line continuation statement inside it. Whatever we add inside a parentheses () will treat as a single statement even it is placed on multiple lines. addition = (10 + 20 + 30 + 40 + 50 + 60 + 70) print(addition) # Output: 280 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 61
  • 62.  Compound statement contain (groups of) other statements; they affect or control the execution of those other statements in some way.  The compound statement includes the conditional and loop statement.  if statement: t is a control flow statement that will execute statements under it if the condition is true. Also known as a conditional statement.  while statement: The while loop statement repeatedly executes a code block while a particular condition is true. Also known as a looping statement.  for statement: Using for loop statement, we can iterate any sequence or iterable variable. The sequence can be string, list, dictionary, set, or tuple. Also known as a looping statement.  try statement: specifies exception handlers.  with statement: Used to cleanup code for a group of statements, while the with statement allows the execution of initialization and finalization code around a block of code. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 62
  • 63.  Python has various simple statements for a specific purpose. 1. Expression Statement 2. The pass statement 3. The del statement 4. The return statement 5. The import statement 6. The continue and break statement Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 63
  • 64.  A combination of operands and operators is called an expression.  The expression in Python produces some value or result after being interpreted by the Python interpreter.  Example: r = a+b res =10+a result = 20+30  An expression in Python can contain identifiers, operators, and operands.  An identifier is a name that is used to define and identify a class, variable, or function in Python. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 64
  • 65. 1.Constant Expressions 2.Arithmetic Expressions 3.Integral Expressions 4.Floating Expressions 5.Relational Expressions 6.Logical Expressions 7.Combinational Expression Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 65
  • 66.  A constant expression in Python that contains only constant values is known as a constant expression.  In a constant expression in Python, the operator(s) is a constant.  A constant is a value that cannot be changed after its initialization.  Example: a = 20 +10 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 66
  • 67.  An expression in Python that contains a combination of arithmetic operators, operands, and sometimes parenthesis is known as an arithmetic expression.  The result of an arithmetic expression is also a numeric value  Example: x = c+d x = c * a Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 67
  • 68.  An integral expression in Python is used for computations and type conversion (integer to float, a string to integer, etc.).  An integral expression always produces an integer value as a resultant.  Example: a = 5 b = 5.0 res = a + int(b) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 68
  • 69.  A floating expression in Python is used for computations and type conversion (integer to float, a string to integer, etc.).  A floating expression always produces a floating-point number as a resultant.  Example: a = 5 b = 5.0 res = float(a) + b Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 69
  • 70.  A relational expression in Python can be considered as a combination of two or more arithmetic expressions joined using relational operators.  The overall expression results in either True or False (boolean result).  We have four types of relational operators in Python (i.e. > , < , >= , <=)(i.e.>,<,>=,<=).  A relational operator produces a boolean result so they are also known as Boolean Expressions.  Example: 10+15>20 Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 70
  • 71.  As the name suggests, a logical expression performs the logical computation using logical operators, and the overall expression results in either True or False (boolean result).  Example: a and b a or b a not b a=3 b=3 if a>0 and b>0: print(“a and b greater than zero”) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 71
  • 72.  As the name suggests, a combination expression can contain a single or multiple expressions which result in an integer or boolean value depending upon the expressions involved.  Example: x = 5 y = 5 res = x + (2 *6) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 72
  • 73.  A boolean expression is an expression that is either true or false.  Example: 5 > 3 True 5 >9 False Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 73
  • 74. Python defines type conversion functions to directly convert one data type to another which is useful in day-to- day and competitive programming. There are two types of Type Conversion in Python:  Implicit Type Conversion  Explicit Type Conversion Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 74
  • 75.  In Implicit type conversion of data types in Python, the Python interpreter automatically converts one data type to another without any user involvement. Program Output x = 10 print("x is of type:",type(x)) y = 10.6 print("y is of type:",type(y)) z = x + y print(z) print("z is of type:",type(z)) x is of type: <class 'int'> y is of type: <class 'float'> 20.6 z is of type: <class 'float'> Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 75
  • 76. 1.int(a, base): This function converts any data type to integer. ‘Base’ specifies the base in which string is if the data type is a string. 2.float(): This function is used to convert any data type to a floating- point number. 3.tuple() : This function is used to convert to a tuple. 4.set() : This function returns the type after converting to set. 5.list() : This function is used to convert any data type to a list type. 6.dict() : This function is used to convert a tuple of order (key,value) into a dictionary. 7.str() : Used to convert integer into a string. 8.complex(real,imag) : This function converts real numbers to complex(real,imag) number. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 76 In Explicit Type Conversion in Python, the data type is manually changed by the user as per their requirement. Various forms of explicit type conversion are explained below:
  • 77. x=5 print("x is of type:",type(x)) y=float(x) print("y is of type:",type(y)) z=str(x) print("z is of type:",type(z)) Output: x is of type: <class 'int’> y is of type: <class ‘float’> z is of type: <class ‘str'> Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 77
  • 78. Solve this : 3 + 4 x 2 – 1 3 + 4 / 2 – 1  BIDMAS  BIDMAS, which stands for Brackets, Indices, Division, Multiplication, Addition and Subtraction. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 78
  • 79.  The operator precedence in Python is listed in the following table. It is in descending order (upper group has higher precedence than the lower ones). Priority Operator Meaning 1 () Parentheses 2 ** Exponent 3 +,-,~ Unary plus, Unary Minus, Bitwise NOT 4 *,/,//,% Multiplication, Division, Floor division, Modulus 5 +,- Addition, Subtraction 6 <<,>> Bitwise shift operators 7 & Bitwise AND 8 ^ Bitwise XOR 9 | Bitwise OR 10 ==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, Identity, Membership operators 11 not Logical NOT 12 and Logical AND 13 or Logical OR Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 79
  • 80. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 80