Professional Documents
Culture Documents
Python Programming Workshop 19-05-2022
Python Programming Workshop 19-05-2022
PYTHON
PROGRAMMING
WORKSHOP
ORGANIZED BY,
DEPT. OF ETC AND MECH OF
SHRI TULJA BHAVANI
COLLEGE OF ENGINEERING,
TULJAPUR
08/03/2023
08/03/2023
Python Overview
2
interpret
Python is instead directly interpreted into machine instructions.
source code output
Hello.py
08/03/2023
History of Python:
5
Python Features
6
Python Environment
8
10
Python Identifiers:
11
Reserved Words:
13
Multi-Line Statements:
15
Statements in Python typically end with a new line. Python does, however,
allow the use of the line continuation character (\) to denote that the line
should continue. For example:
total = item_one + \
item_two + \
item_three
Statements contained within the [], {}, or () brackets do not need to use the
line continuation character. For example:
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
08/03/2023
Quotation in Python:
16
Python accepts single ('), double (") and triple (''' or """)
quotes to denote string literals, as long as the same type of
quote starts and ends the string.
The triple quotes can be used to span the string across
multiple lines. For example, all the following are legal:
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and
sentences."""
08/03/2023
Comments in Python:
17
18
Multiple Assignment:
23
Dictionary
Boolean
Set
Sequence type
08/03/2023
Python Numbers:
25
Number data types store numeric values. They are immutable data types,
which means that changing the value of a number data type results in a
newly allocated object.
Number objects are created when you assign a value to them. For example:
var1 = 1
var2 = 10
Python supports four different numerical types:
int (signed integers)
long (long integers [can also be represented in octal and hexadecimal])
float (floating point real values)
complex (complex numbers)
08/03/2023
Number Examples:
26
Python Strings:
27
Example:
28
Output:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
08/03/2023
Python Lists:
29
Lists are the most versatile of Python's compound data types. A list
contains items separated by commas and enclosed within square brackets
([]).
To some extent, lists are similar to arrays in C. One difference between
them is that all the items belonging to a list can be of different data type.
The values stored in a list can be accessed using the slice operator ( [ ] and
[ : ] ) with indexes starting at 0 in the beginning of the list and working
their way to end-1.
The plus ( + ) sign is the list concatenation operator, and the asterisk ( * )
is the repetition operator.
08/03/2023
30
Output:
['abcd', 786, 2.23, 'john', 70.2]
abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
08/03/2023
Python Tuples:
31
32
OUTPUT:
('abcd', 786, 2.23, 'john', 70.2)
abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
08/03/2023
Python Dictionary:
33
34
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two“
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
OUTPUT:
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
08/03/2023
Python Operators
36
Arithmetic operators
Comparison operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
08/03/2023
Arithmetic Operators
37
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 = 10
/ (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 =
(Multiplication) 10 => a * b = 200
% (reminder) It returns the reminder after dividing the first operand by the second operand.
For example, if a = 20, b = 10 => a%b = 0
** (Exponent) It is an exponent operator represented as it calculates the first operand power
to the second operand.
//(Floor It gives the floor value of the quotient produced by dividing the two
division) operands.
08/03/2023
Comparison operator
38
Operator Description
Assignment Operators
39
Operator Description
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.
08/03/2023
Bitwise Operators
40
Operator Description
&(binary If both the bits at the same place in two operands are 1,
and) then 1 is copied to the 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 The resulting bit will be 1 if both the bits are different;
xor) 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 vice versa.
<<(left The left operand value is moved left by the number of bits
shift) present in the right operand.
>>(right The left operand is moved right by the number of bits
shift) present in the right operand.
08/03/2023
Logical Operators
41
Operator Description
Membership Operators
42
Operator Description
Identity Operators
43
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.
08/03/2023
Statement Description
The if statement
45
Example:
num = int(input("enter the number?"
))
if num%2 == 0:
print("Number is even")
Output:
enter the number?10
Number is even
08/03/2023
Example 1
Program
number = int(input("Enter the number?"))
if number==10:
print("number is equals to 10")
elif number==50:
print("number is equal to 50");
elif number==100:
print("number is equal to 100");
else:
print("number is not equal to 10, 50 or 100");
Output:
Enter the number?15
number is not equal to 10, 50 or 100
08/03/2023
Python Loops
48
Loop Description
Statement
for loop The for loop is used in the case where we need to execute some part
of the code until the given condition is satisfied. The for loop is also
called as a per-tested loop. It is better to use for loop if the number
of iteration is known in advance.
while loop The while loop is to be used in the scenario where we don't know
the number of iterations in advance. The block of statements is
executed in the while loop until the condition specified in the while
loop is satisfied. It is also called a pre-tested loop.
do-while loop The do-while loop continues until a given condition satisfies. It is
also called post tested loop. It is used when it is necessary to execute
the loop at least once (mostly menu driven programs).
08/03/2023
Output: *
**
0123456789 ***
****
*****
08/03/2023