Unit 1
Unit 1
By,
Ms. Swapnali S. Gawali,
Asst. Prof. Computer Engineering
Sanjivani College of Engineering, Kopargaon
UNIT-I CONTENT
2
WHAT IS PYTHON?
3
HISTORY OF PYTHON
4
FEATURES OF PYTHON
2. Easy to 3. Easy to
1. Free and code Read 4. Object-Oriented
Open Source Language
6. High-Level
9. Integrated and Language
Interpreted
8. Easy to
7. Portable
Debug
5
VALUES
6
DATA TYPE
7
NUMERIC DATA TYPE
9
CONTINUE..
10
CONTINUE..
11
BOOLEAN
12
SET
13
DICTIONARY
14
VARIABLES
15
RULES FOR CREATING VARIABLES IN PYTHON
17
OUTPUT FUNCTION
18
OUTPUT FUNCTION (CONTINUE)
• Example 2:
name='Sonal’
rollno=10 Output
Name is Sonal
print('Name is ',name) Roll no is 10
print('Roll no is ',rollno)
Example 3:
name='Sonal’
Output
rollno=10 Name is Sonal Roll no is 10
print('Name is ',name, 'Roll no is ',rollno)
19
OUTPUT FUNCTION (CONTINUE)
• Example 4:
name='Sonal’
Output
rollno=10 Name is Sonal. Roll no is 10.
print(f'Name is {name}. Roll no is {rollno}.')
20
OUTPUT FUNCTION (CONTINUE)
• Syntax of print()
print(object(s), sep=separator, end=end, file=file, flush=flush)
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
21
OUTPUT FUNCTION (CONTINUE)
22
OUTPUT FUNCTION (CONTINUE)
23
INPUT FUNCTION
print(f'Name is {name}.’)
print(type(name))
24
INPUT FUNCTION
• Example:
name=input('Enter your name’)
Output
print(f'Name is {name}.’) Enter your nameSonal
print(type(name)) Name is Sonal.
rollno=int(input('Enter roll no’)) <class 'str’>
Enter roll no10
#int() use to convert string to integer value Roll no is 10.
print(f'Roll no is {rollno}.’) <class 'int'>
print(type(rollno))
25
OPERATORS AND OPERANDS
A+B-5
Operands
26
DIFFERENT OPERATOR IN PYTHON
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Identity operators
6. Membership operators
7. Bitwise operators
27
ARITHMETIC OPERATORS
28
PROGRAM
a=10
b=20
c=a+b
print(“Addition is ”,c) Output:
c=a-b Addition is 30
Subtraction is -10
print(“Subtraction is ”,c) Multiplication is 200
Division is 0.5
c=a*b
print(“Multiplication is ”,c)
c=a/b
print(“Division is ”,c)
29
ASSIGNMENT OPERATORS
+= 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
30
PROGRAM
a=10
b=20
print("a is ",a)
print("b is ",b)
a += 1 Output:
a is 10
print("Add 1 to a ",a) b is 20
b -= 1 Add 1 to a 11
Subtract 1 from b 19
print("Subtract 1 from b ",b) a square is 121
a **= 2
print("a square is ", a)
31
COMPARISON OPERATORS
32
PROGRAM
x=10
y=20
print(x==y)
Output:
print(x!=y) False
print(x<y) True
print(x>y) True
False
print(x<=y)
True
print(x>=y) False
33
LOGICAL OPERATORS
34
PROGRAM
x=10
y=20
print(x==y and x<y) Output:
False
print(x!=y or x>y) True
False
print(not(x<y))
35
IDENTITY OPERATORS
• 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:
36
PROGRAM
x=10
y=10 Output:
print(x is y) True
False
print(x is not y)
37
MEMBERSHIP OPERATORS
38
PROGRAM
x='sanjivani’
print('s' in x)
print('s' not in x) Output:
True
print('S' in x) False
print('S' not in x) False
y='anji’ True
True
print(y in x) False
print(x in y)
39
BITWISE OPERATOR
| Bitwise OR x|y
~ Bitwise NOT ~x
41
BITWISE OR OPERATOR
42
BITWISE XOR OPERATOR
• 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)
43
PROGRAM
a = 10
b=4
print("a ^ b =", a ^ b)
44
BITWISE RIGHT SHIFT
• 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
45
BITWISE LEFT SHIFT
• 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
46
PROGRAM
a = 10
b = -10
print("a >> 1 =", a >> 1) Output:
print("b >> 1 =", b >> 1) a >> 1 = 5
b >> 1 = -5
a=5 a << 1 = 10
b = -10 b << 1 = -20
47
STATEMENT
add=20+30+\
50+60+\
90
print(add)
Output: 250
49
IMPLICIT CONTINUATION:
addition = (10 + 20 +
30 + 40 +
50 + 60 + 70)
print(addition)
# Output: 280
50
COMPOUND STATEMENTS
• 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.
51
SIMPLE STATEMENTS
52
EXPRESSION IN PYTHON
1.Constant Expressions
2.Arithmetic Expressions
3.Integral Expressions
4.Floating Expressions
5.Relational Expressions
6.Logical Expressions
7.Combinational Expression
54
CONSTANT EXPRESSION
55
ARITHMETIC EXPRESSION
56
INTEGRAL EXPRESSIONS
57
FLOATING EXPRESSIONS
58
RELATIONAL EXPRESSIONS
59
LOGICAL EXPRESSIONS
• 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”)
60
COMBINATIONAL EXPRESSIONS
61
BOOLEAN EXPRESSIONS
62
CONVERSION FUNCTIONS
63
IMPLICIT TYPE CONVERSION
64
EXPLICIT TYPE CONVERSION
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:
1.int(a, base): This function converts any 5.list() : This function is used to convert any
data type to integer. ‘Base’ specifies data type to a list type.
the base in which string is if the data
6.dict() : This function is used to convert a
type is a string.
tuple of order (key,value) into a
2.float(): This function is used to dictionary.
convert any data type to a floating- 7.str() : Used to convert integer into a
point number. string.
3.tuple() : This function is used to convert 8.complex(real,imag) : This
to a tuple. function converts real numbers to
4.set() : This function returns the type complex(real,imag) number.
after converting to set. 65
PROGRAM
x=5
print("x is of type:",type(x))
y=float(x)
Output:
print("y is of type:",type(y)) x is of type: <class 'int’>
y is of type: <class ‘float’>
z=str(x) z is of type: <class ‘str'>
print("z is of type:",type(z))
66
PRECEDENCE OF OPERATORS
Solve this :
3+4x2–1
3+4/2–1
• BIDMAS
• BIDMAS, which stands
for Brackets, Indices, Division, Multiplication, Addition
and Subtraction.
67
• 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
68
THANK YOU..
69