3. PRINT FUNCTION
items = [ 1, 2, 3, 4, 5]
for item in items:
print(item)
Output:
1
2
3
4
5
items = [ 1, 2, 3, 4, 5]
for item in items:
print(item, end=’ ‘)
Output:
1 2 3 4 5
4. OPERATORS
Addition + Subtraction -
Multiplication * Exponentiation **
Division / Integer division / /
Remainder %
Binary left shift << Binary right shift >>
And & Or |
Less than < Greater than >
Less than or equal to <= Greater than or equal to >=
Check equality == Check not equal !=
5. PRECEDENCE OF OPERATORS
Parenthesized expression ( ….. )
Exponentiation **
Positive, negative, bitwise not +n, -n, ~n
Multiplication, float division, int division, remainder *, /, //, %
Addition, subtraction +, -
Bitwise left, right shifts <<, >>
Bitwise and &
Bitwise or |
Membership and equality tests in, not in, is, is
not, <, <=, >, >=, !=, ==
Boolean (logical) not not x
Boolean and and
Boolean or or
Conditional expression if ….. else
6. PRECEDENCE OF OPERATORS
Examples:
a = 20
b = 10
c = 15
d = 5
e = 2
f = (a + b) * c / d
print( f)
g = a + (b * c) / d - e
print(g)
h = a + b*c**e
print(h)
7. 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.
8. SPECIAL USE OF + AND *
Examples:
x = "Python is "
y = "awesome."
z = x + y
print(z)
Output:
Python is awesome.
print(‘It is’ + 2*’very ’ + ’hot.’)
Output:
It is very very hot.
9. 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
11. # Write a python program to open and read a file
a=open(“one.txt”, ”r”)
content = a.read()
print(content)
a.close( )
# Write a python program to open and write “hello world” into a file.
f=open("file.txt","a")
f.write("hello world")
f.close( )
12. # Python program to write the content “Hi python programming” for the existing file.
f=open("MyFile.txt",'w')
f.write("Hi python programming")
f.close()
# Write a python program to open and write the content to file and read it.
f=open("abc.txt","w+")
f.write("Python Programming")
print(f.read())
f.close()