Under the title 'Fundamental of Python' we will discuss two basics of Python program. These are the building blocks of Python program.
1. Tokens and 2. Comments
1. Tokens: The smallest individual unit in a program is referred as 'Tokens'. It can be categorised as:
Identifiers: Identifiers are the basic building blocks of a program. These are the names of the different elements in a program such as variables. These elements have specific properties that help in making a program.
There are some rules to create identifiers in Python. These are
i. These may be the combination of alphabet in lowercase (a-z), uppercase (A-Z), digits (0-9) and underscore (_).
ii. The first character of an identifier must be a letter or an underscore, it must not be a digit.
iii. It must not contain any special character (like $ % ^ & *) except underscore (_).
iv. It must not be a 'keyword'.
Some examples of valid identifiers are: hello, _hello, Hello, _123, a123, _Hello etc.
Here are some examples of invalid identifiers: 1Hello, @hello, return
Can you identify the reason, why these are invalid identifiers. Match you answer to click on answer button below:
false |
return |
else |
lambda |
yield |
from |
def |
none |
import |
is |
with |
break |
or |
if |
true |
elif |
while |
assert |
for |
continue |
raise |
and |
in |
as |
finally |
not |
global |
async |
del |
try |
except |
nonlocal |
class |
pass |
await |
- Integer Literals: Integer Literals are whole number without any fractional part. It may be positive or negative. For example: 5, 15, -150 etc
- Character Literals: Character Literals are single characters that are enclosed in single quotes. For example: 'x', '9' etc
- Floating Literals: Floating Literals are fractional numbers. These may be positive or negative. For example: 34.876, 9.8766, -1.344 etc
- String Literals: A string literal is a sequence of characters enclosed within single or double quotes. For example: 'Hello', " Welcome to Python" etc.
Operator |
Name of Operator |
Purpose |
Example I |
Example II |
+ |
Addition |
Sum of two operands (a+b).
Concatenation or joining of two string operands. |
>>>6+9 Output: 15 |
>>>’Hello’ + ‘World’ Output: HelloWorld |
- |
Subtraction |
Difference between the two
operands (a-b). |
>>>7-4 Output: 3 |
>>>4-7 Output: -3 |
* |
Multiplication |
Product of two operands (a*b)
or print the same string as given number of times. |
>>>5*6 Output: 30 |
>>>’Hello’*3 Output: HelloHelloHello |
/ |
Division |
Quotient of two operands
(a/b) |
>>>5/4 Output: 1 |
>>>13/3 Output: 4.33333 |
// |
Floor Division |
Quotient of the two operands
without fractional part (a//b) |
>>>5//4 Output: 1 |
>>>4.3//3 Output: 1.0 |
% |
Modulus |
Integer Remainder: after
division of ‘a’ by ‘b’ (a%b) |
>>>5%4 Output: 1 |
>>>7%2 Output: 1 |
** |
Exponent |
Product of ‘a’ by itself ‘b’
times (a to the power of b) (a**b) |
>>>5**4 Output: 625 |
>>>4**.5 Output: 2.0 |
Name of Relational Operator |
Symbol used in Python |
Meaning |
Example |
Greater than |
> |
True if left operand is
greater than the right |
x >
y |
Less than |
< |
True if left operand is less
than the right |
y <
x |
Equal to |
= = |
True if left operand is equal
to the right |
x =
= y |
Not equal to |
! = |
True if left operand is not equal
to the right |
x !
= y |
Greater than or equal to |
>
= |
True if left operand is
greater or equal to the right |
x >
= y |
Less than or equal to |
<
= |
True if left operand is less
or equal to the right |
y <
= y |
Name of Logical Operator |
Denoted by |
Example |
Logical AND |
and |
a
and b |
Logical OR |
or |
a
or b |
Logical NOT |
not |
not
a |