Basics of Python
Basics of Python
• Long Integers
Bigger whole numbers.
For eg: 5694412365366L
A long integer must have ‘l’ or ‘L’ as suffix.
Numerical Constants
• Floating Point Numbers
eg: 5.28 or 25.9E-4
Here, E represents power of 10.
25.9E-4 = 25.9 x 10-4
• Complex Numbers
Numbers of the form a+bi.
For eg: -5+6i
Numerical Constants
Note: Commas are never used in numeric values.
Values like 3,567 are not allowed in Python.
format( ) Function
• It produces a string version of a floating point value rounded to a
specific number of decimal places.
Output Output
format( ) Function
• The format( ) function can also be used for inserting commas in the
numbers.
Output Output
Simple Operations on Numbers
1. + : Addition.
2. - : Subtraction.
3. / : divides and returns quotient in float type.
4. // : divides and returns quotient in desired data type. It is known as
floor division operator.
5. % : divides and returns the remainder. It is known as modulo operator.
6. ** : exponents. (5**2 is equivalent to 52 and will return 25 as answer)
7. Division by zero : Invalid. It will generate an error.
Strings
• Strings are declared in single quotes or double quotes.
Example:
Output Output
Strings
• We can enter a multi-line string using triple quotes.
Example:
Output
Strings
• Python concatenates two strings placed side by side.
Example:
Output
Output
Escape Sequences
• Such characters must be escaped by placing a backslash before them.
Example:
Output
Escape Sequences
Escape Sequence Purpose Syntax Output
\\ Prints backslash print(“\\”) \
\’ Prints single quote print(“\’”) ‘
\” Prints double quote print(“\””) “
\n Prints newline print(“Hello\nWorld”) Hello
World
\t Inserts a tab space Print(“Hello\tWorld”) Hello World
String Formatting
It will right align the string in a
field width of 10 characters.
Output
• We can even create our own data types (like classes). It will be discussed
in upcoming chapter.
Assigning or Initializing Values to Variables
Output
Reassigning Values to Variables
Output
input( ) Function
input function is used to take input from
the user. The text inside the double quote
will be displayed as prompt while taking
the input.
Output
input from the user.
Output Output
Output
Bitwise Operators
• They perform operations at the bit level.
• Various Bitwise Operators are:
1. Bitwise AND (&)
2. Bitwise OR ( | )
3. Bitwise XOR ( ^ )
4. Bitwise NOT ( ~ )
Bitwise Operators
1. Bitwise AND (&)
Convert the given numbers into Binary of 4 bits and then follow the truth
table given below.
A B A&B
0 0 0
1 0 0
0 1 0
1 1 1
Bitwise Operators
1. Bitwise AND (&)
Example:
print(5&6) will be evaluated by first converting 5 into binary which
will be 0101 and 6 in binary will be 0110.
Now using the truth table of & operator evaluate corresponding digits.
5&6 = 0101 & 0110 = 0100
Now 0100 in binary = 4 in decimal.
Thus, print(5&6) will give output = 4.
Bitwise Operators
1. Bitwise AND (&)
Output
Bitwise Operators
2. Bitwise OR ( | )
Convert the given numbers into Binary of 4 bits and then follow the truth
table given below.
A B A|B
0 0 0
1 0 1
0 1 1
1 1 1
Bitwise Operators
2. Bitwise OR ( | )
Example:
print(5|6) will be evaluated by first converting 5 into binary which
will be 0101 and 6 in binary will be 0110.
Now using the truth table of | operator evaluate corresponding digits.
5|6 = 0101 | 0110 = 0111
Now 0111 in binary = 7 in decimal.
Thus, print(5|6) will give output = 7.
Bitwise Operators
2. Bitwise OR ( | )
Output
Bitwise Operators
3. Bitwise XOR ( ^ )
Convert the given numbers into Binary of 4 bits and then follow the truth
table given below.
A B A^B
0 0 0
1 0 1
0 1 1
1 1 0
Bitwise Operators
3. Bitwise XOR ( ^ )
Example:
print(5^6) will be evaluated by first converting 5 into binary which
will be 0101 and 6 in binary will be 0110.
Now using the truth table of ^ operator evaluate corresponding digits.
5^6 = 0101 ^ 0110 = 0011
Now 0011 in binary = 3 in decimal.
Thus, print(5^6) will give output = 3.
Bitwise Operators
3. Bitwise XOR ( ^ )
Output
Bitwise Operators
4. Bitwise NOT ( ~ )
Convert the given numbers into Binary of 4 bits and then follow the truth
table given below.
A !A
0 1
1 0
Bitwise Operators
4. Bitwise NOT ( ~ )
The NOT bitwise operation inverts bits. A 0 becomes a 1. A 1 becomes a
0.
When numbers are printed in base-10, the result of a NOT operation can
be surprising. In particular, positive numbers can become negative and
negative numbers can become positive.
Bitwise Operators
4. Bitwise NOT ( ~ )
For example:
~ 5 will give -6
At the bit level:
~ 0000 0101 (5) = 1111 1010 (-6)
Output
Shift Operators
• Shift Operators refer to bit shifting.
• Python supports two bitwise shift operator:
1. Shift Left (<<)
2. Shift Right (>>)
Shift Operators
1. Shift Left (<<)
When shifting left, the most-significant bit is lost, and a 0 bit is inserted on
the other end.
Output Output
Shift Operators
2. Shift Right (>>)
When shifting right, the least-significant bit is lost, and a 0 bit is inserted
on the other end.
A single right shift divides a binary number by 2 and leaves the remainder:
For Example:
5>>1 = 0101 >> 1 = 0010 = 2
Therefore, 5>>1 = 5\21 = 2;
5>>2 = 5\22= 1; 5>>3 = 5\23 = 0
Shift Operators
2. Shift Right (>>)
Output Output
Logical Operators
1. Logical AND (&&)
If we have an expression (a>b) && (b>c), then the whole expression
is true only if both the expressions are individually true.
2. Logical OR (||)
If we have an expression (a>b) || (b>c), then the whole expression
is true if any one of the two expressions is individually true.
3. Logical NOT ( ! )
It converts any non-zero number to 0 and 0 to 1.
Membership Operators
1. in Operator:
It returns True if a variable is found in a specific sequence.
Otherwise, it will return False.
For example:
Output Output
Membership Operators
2. not in Operator:
It returns True if a variable is not found in a specific sequence.
Otherwise, it will return False.
For example:
Output Output
Identity Operator
1. is Operator:
It returns True if values on both sides of the operator point to the
same object. Otherwise, it will return False.
Output Output
Identity Operator
2. is not Operator:
It returns True if values on both sides of the operator does not
point to the same object. Otherwise, it will return False.
Output Output
The End