Python 3 Part 2 - Number system, operators, decision making and loops
Table of content
Number System
The python number system is representing the way of using the numbers in language.
- Decimal Number System - In decimal system numbers are represented in base 10.
-
To convert a number into decimal system we can use inbuilt function
int()
- int(<number in string>, base)
- The first parameter is the number in string format
- The second parameter is the base of the number
- Binary Number System - In binary system the numbers are represented in base 2. '
- To convert a number into binary system we can use inbuilt function bin()
- bin(<number>)
-
It takes one parameter, the number in the source system. Not a
string format.
- Octal Number system - In octal the numbers are represented in base 8.
- To convert a number into octal number system we can use inbuilt function oct()
- oct(<number>)
- It takes one parameter, the number in the source system. Not a string format.
- Hexadecimal Number System - The numbers are represnted in base 16.
- To convert a number into hexadecimal system we can use inbuilt function hex()
- hex(<number>)
- It takes one parameter, the number in the source system. Not a string format.
Tip: If you are using Python IDLE, you can go back to previous by typing Alt + P keys on a windows machine.
Operators
bitwise operator
tilde (NOT) operator (~)
The tilde (~) operator is a bit-wise negation operator. It flips the
bits of a number. The binary representation of the positive number starts
with most significant bit(MSB) as 0. The binary representation of the
negative number is in 2' complement form. The MSB in this case is 1, hence
interpreted as 2' complement form of the number.Note: ~ operator only inverts the bit pattern of the number. The number it represented post ~ operation depends on the bit pattern. The easy way to remember would be add +1 and change the sign.
Bitwise AND operator (&)
The bitwise AND operator returns 1 if both the bits are 1 else 0.
The variable a = 4, bit pattern as 0000 0100-----------------------------------------
Bitwise OR operator (|)
The bitwise OR operator returns 1 if either of the bit is 1 else 0.
The variable a = 4, bit pattern as 0000 0100-----------------------------------------
Bitwise XOR operator (^)
The bitwise XOR operator returns 1 if one of the bits is 1 and the other is 0 else returns false
The variable a = 4, bit pattern as 0000 0100-----------------------------------------
Bitwise left shift operator (<<)
The bitwise left shift operator shifts the bit pattern by n. Add 0 in the
bits.
Bitwise right shift operator (>>)
The bitwise right shift operator shifts the bit pattern by n. Add MSB in
the bits.logical operator
Python has logical operators that are used on conditional statements either True or False.logical OR operator
Logical OR operator returns True if either or both the operands are True else it returns False.
logical AND operator
Logical AND operator returns True if only both the operands are True else it returns False.
logical NOT operator
Logical NOT operator returns True if the boolean value is False and vice-versa.
Importing modules
We can also import with alias or import specific function from the module external module.
User input
To give user input values to the program we can use input() method, we enter the input value. input() method returns a string value. We typecast it using int() method.run from terminal
Decision making
Python provides decision making contruct if-elif to execute different flows
in code. Syntax below:
Below example shows displays the input number is odd or even. Save the file
as odd_even.py
The output of the above code is
Note : Take care of indentations
Loops
Loop contruct in python allows us to run a piece of logic in a loop until a break condition.
while loop
Syntax for while loop below. The condition evaluates to true keeps the while loop running, when the condition becomes false the while loop exits.
Example:
for loop
for loop contruct in python allows us to run a piece of logic in a loop until a break condition.i is the element in the iterable object.
Jump Statement
continue statement skip the current iteration.
pass statement do nothing.