An identifier is a name given to entities like class, functions, methods, variables, etc. It helps to differentiate one entity from another.
Rules for writing identifiers
Identifiers can be a combination of:
Lowercase (a to z)
Uppercase (A to Z)
Digits (0 to 9)
An underscore_
Examples: myClass, var_1, print_this_to_screen.
An identifier cannot start with a digit:
Example: 1variable is invalid, but variable1 is a valid
Keywords cannot be used as identifiers
We cannot use special symbols like !, @, #, $, % etc. in our identifier
An identifier can be of any length.
myClass=1
1variable=10
True=10
variable$=10
variable=10
Variable=100
print(variable)
print(Variable)
Python Keywords: Keywords are the reserved words in Python.
True | False | None | import | class |
if | else | elif | for | while |
try | except | finally | raise | return |
break | continue | pass | def | lambda |
from | with | is | in | not |
as | and | or | del | yield |
await | asert | async | global | nonlocal |
Python interprets a sequence of decimal digits without any prefix to be a decimal number.
Prefix | Interpretation | Base |
---|---|---|
0b (zero + lowercase letter 'b') | Binary | 2 |
0B (zero + lowercase letter 'B') | Binary | 2 |
0o (zero + lowercase letter 'o') | Octal | 8 |
0O (zero + lowercase letter 'O') | Octal | 8 |
0x (zero + lowercase letter 'x') | Hexadecimal | 16 |
0X (zero + lowercase letter 'X') | Hexadecimal | 16 |
The float type in Python designates a floating-point number. Float values are specified with a decimal point.
Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation:
Complex numbers are specified as: real part + (imaginary part) j
Example: 2+3j
Strings are sequences of character data.
A string in Python can contain as many characters as we wish. The only limit is our machine’s memory resources. A string can also be empty:
Use the escape \ sequence if you want to include a quote character as part of the string itself.
print('It\'s my life')
print("The \"Honey Cake\" is delicious")
print('It's my life')
print("The "Honey Cake" is delicious")
print('a \
is an \
alphabet')
print('First Line\nSecond Line\nThird Line')
print('hello')
print("Hello")
a=10
b=20
print(a)
print(b)
a,b=b,a
print(a)
print(b)
sum=a+b
print(sum)
a = 20
b = 10
c = 15
d = 5
e = (a + b) * c / d #( 30 * 15 ) / 5 = 90
print ("Value of (a + b) * c / d is ", e)
e = ((a + b) * c) / d # (30 * 15 ) / 5=90
print ("Value of ((a + b) * c) / d is ", e)
e = (a + b) * (c / d); # ________=__
print("Value of (a + b) * (c / d) is ", e)
e = a + (b * c) / d; # ______=__
print ("Value of a + (b * c) / d is ", e)
if, if else, if-elif-else, Nested if statements are used whenever we want to make a decision based on some conditions. The syntax for decision making is, the line ends with : either after else or after the condition is mentioned in the if block. The segment of code which needs to be executed as part of the decision is written with indentation.
If the condition given in if block are true then, first only the code written with indentation are executed and then the remaining code will execute. If the condition is false then the code written in if block is skipped and remaining code will be executed.
a=20
if(a>10):
print('Inside if block')
print('a is bigger than 10')
print('After if statement')
if...else statements are used when we want to make any one decision out of two based on some condition. If the condition is true the code in the if block will be executed. If the condition is false then else block will be executed. Once, any one of the code block (either if or else) is executed, the remaining code wil be executed.
a = 20
b = 33
if b > a:
print('Inside if block')
print("b is greater than a")
else:
print('Inside else block')
print("b is not greater than a")
print('Outside if else block')
a = 20
b = 33
if a > b :
print('Inside if block')
print("b is greater than a")
else:
print('Inside else block')
print("a is not greater than b")
print('Outside if else block')
if else statements are used when we need to check two conditions but if there are multiple conditions which needs to be checked then we use if elif statements. Here, else block is optional. else block will be executed if none of the if conditions are true.
percentage=55
if percentage > 70:
print("Distinction")
elif ( percentage > 60 and percentage < 70 ) :
print("First class")
elif ( percentage > 50 and percentage < 60 ) :
print("Second class")
elif ( percentage > 35 and percentage < 50 ) :
print("Pass class")
else:
print("Fail")
print('Outside if elif statements')
a = 200
b = 200
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Python supports nested structures. Which means we can have if statement inside another if statement.
x =40
if x > 10:
print('inside outer if statement')
print("Above ten,")
if x > 20:
print("and also above 20!")
print('Inside if block of inner if statement')
else:
print("but not above 20.")
print('Inside else block of inner if statement')
else:
print('a is less than 10')
print('outside if statement')
x =5
if x > 10:
print('inside outer if statement')
print("Above ten,")
if x > 20:
print("and also above 20!")
print('Inside if block of inner if statement')
else:
print("but not above 20.")
print('Inside else block of inner if statement')
else:
print('a is less than 10')
print('outside if statement')
If there is any need to execute a set of instructions several times as long as certain condition is true, we use looping structures such as for loops, while loops, and nested loops.
It has the ability to iterate over the items of any sequence, such as a list or a string.
for i in 'Hello World':
print(i)
for i in (10,20,30,40,50):
print(i)
for n in range(0,10):
print(n)
language=['C','C++','Java','Python']
for index in language:
print(index)
language=['C','C++','Java','Python']
for index in range(len(language)):
print(language[index])
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
While loop is executed as long as the condition is true.
n=1
while n<10:
print(n)
n=n+1
range ( ) is a built-in function of Python. It is used when a user needs to perform an action for a specific number of times
numbers = range(5)
for n in numbers:
print(n)
numbers = range(2,5)
for n in numbers:
print(n)
numbers = range(1,5,2)
for n in numbers:
print(n)
break is used to control the sequence of the loop. If we want to terminate a loop and skip to the next code after the loop then we use break statement. And if we want to continue in the loop in such case we use continue statement
pass statement are null statements which are ignored by the compilers. Usually we use pass statements as placeholder.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
for x in [0, 1, 2]:
pass
for x in range(6):
print(x)
else:
print("Finally finished!")
#### Note: The else block will NOT be executed if the loop is stopped by a break statement.
for x in range(6):
if x == 3:
break
print(x)
else:
print("Finally finished!")
x = 'print(55)'
eval(x)