Python_M1_P3
Python_M1_P3
HUDA NOORDEAN
Department of CSE
College of Engg. Trikaripur
Why should we learn Python?
The program that translates Python instructions and then executes them is the
Python interpreter. When we write a Python program, the program is executed
by the Python interpreter. This interpreter is written in the C language.
Python IDLE
Python interpreter is embedded in a number of larger programs that make it
particularly easy to develop Python programs. Such a programming environment is
IDLE
( Integrated Development and Learning Environment).
Running Python
1) In interactive mode:
>>> print("Hello Students")
Hello Students
>>> a=10
>>> print(a)
10
>>> x=10
>>> z=x+20
>>> z
30
Interactive shell
Running Python
2) In script mode:
Programmers can store Python script source code in a file
with the .py extension, and use the interpreter to execute the
contents of the file.
>>>x=input('Enter number : ‘)
Output:
Enter number : 5
Python Keywords
Python has a set of keywords that are reserved words that cannot be
used as variable names, function names, or any other identifiers:
Python has 35 keywords:
OPERATORS AND OPERANDS
• Operators are used to perform basic
operation
>>>a=1
>>>b=2
>>>b+a
3
• Here, + is operator, a and b are operands.
• Python supports following operators
3. Logical Operators
1. Arithmetic Operators
2. Relational / Comparison Operators 4. Bitwise Operators
3. Assignment Operator 5. Membership Operators
6. Identity Operators
Arithmetic Operators
• It performs operations like addition, subtraction, multiplication, division, modulus etc.
• Assume a=10 and b=20
Relational/Comparison Operators
• These operators compare the operands like greater than, less than, equal to etc.
Assignment Operator
• Assignment operator symbol = is used in program to assign value to a variable.
• Example: x=1 means value 1 is assigned to variable x
• Similarly, x+ = 5 means x = x+5
Logical Operators
3. not
Operand A not A
not operator is used TRUE FALSE
to invert the truth
FALSE TRUE
table.
Bitwise Operators
| Binary or
^ Binary Xor
Example 2:
g = a + (b * c) / d – e // 48.0
print(g)
h = a + b*c**e
print(h) // 2270
Multiple Assignment
Python allows you to assign a single value to several variables
simultaneously.
a = b = c = 1.5
a, b, c = 1, 2, " Red“
Here, two integer objects with values 1 and 2 are assigned to
variables a and b respectively and one string object with the
value "Red" is assigned to the variable c.
STRINGS
Output: a
Special Use of + and * on strings
Examples:
print(2*' very ')
x = "Python is "
Output:
y = "awesome."
very very
z=x+y
print('It is',2*' very ','hot')
print(z)
Output:
Output:
It is very very hot.
Python is awesome.
Use of \”, \n, \t
Specifying a backslash (\) in front of the quote character in a
string “escapes” it and causes Python to suppress its usual
special meaning. It is then interpreted simply as a literal single
quote character:
>>> print(" \”Beauty of Flower\” ")
”Beauty of Flower”
>>> print('Red \n Blue \n Green ')
Red
Blue
Green
>>> print("a \t b \t c \t d")
a b c d
Comments
Single-line comments begins with a hash ( # ) symbol and is useful in mentioning that
the whole line should be considered as a comment until the end of line.
A Multi line comment is useful when we need to comment on many lines. In python,
triple double quote(“ “ “) and triple single quote(‘ ‘ ‘)are used for multi-line
commenting.
Example:
’’’ My Program to find
Average of three numbers ’’’
a = 29 # Assigning value of a
b = 17 # Assigning value of b
c = 36 # Assigning value of c
average = ( a + b + c)/3
print(‘Average value is ‘, average)
Thank You