Skip to main content

Python 3 Part 2 - Number system, operators, decision making and loops

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
For example: a binary number 0b100 is converted into decimal system. Similarly we can convert numbers from octal and hexadecimal into decimal system.
 
  • 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.
Below example show, an octal number 0o3 is converted into binary 0b11. A decimal number 3400 is converted to binary ob110101001000. And a hexadecimal number 0xFFF is converted into binary as 0b111111111111. 
 
  • 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.
Below example explains, a decimal number 20 converted to its octal representation 0o24. A binary number 0b100 is converted into octal 0o4 and a hexadecimal number 0xFFF is converted into octal system as 0o7777.

  • 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
The variable b =12, bit pattern as 0000 1100
                    -----------------------------------------
The variable c = 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
The variable b =12, bit pattern as 0000 1100
                    -----------------------------------------
The variable d = 12,bit pattern as 0000 1100 
 

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
The variable b =12, bit pattern as 0000 1100
                    -----------------------------------------
The variable e = 8,  bit pattern as 0000 1000 

Bitwise left shift operator (<<)

The bitwise left shift operator shifts the bit pattern by n. Add 0 in the bits.
Number 10 left shift by n as , 10 << n. 
The variable a = 4, left shift by 1 bit as below.
The variable a = 4, bit pattern as 0000 0100 << 1
                    -----------------------------------------
The variable f = 8,  bit pattern as 0000 1000 

Bitwise right shift operator (>>)

The bitwise right shift operator shifts the bit pattern by n. Add MSB in the bits.
Number 10 right shift by n as , 10 >> n. 
The variable a = 4, right shift by 1 bit as below.
The variable a = 4, bit pattern as 0000 0100 >> 1
                    -----------------------------------------
The variable g = 2,  bit pattern as 0000 0010 

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

To import other modules we use the import keyword. For example importing a math module. The math module implements mathematical functions that help in modular and reuse of code.

 


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.
 

Another way of giving input values is through command prompt using sys module. Write the below code in a file as save as argument_input.py

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

break statement breaks from the loop.
continue statement skip the current iteration.
pass statement do nothing.

range function 

range function returns an object of range class <class 'range'> which can be used along with for loop to iterable over the data. If you want to use range function with a user defined data structure then it has to extend iterable behavior.

Thanks you folks.

Most viewed

Ruby on rails part 6 - Blocks , lambda, procs and closure

 Blocks , lambda, procs  and closure Table of content  1. Blocks 2. Lambda 3. Procs 4. Closure Blocks  Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or between brackets {} 
. Blocks can have multiple arguments
. The argument names are defined between two pipe | characters. Blocks are typically used with ‘each’ method which iterates over a list. Syntax of block using {} ['List of items'].each { | block arguments|  block body }  Syntax of block using do-end ['List of items'].each do | block arguments |      # block body end Example of block declared as do-end with each method.   [ 1 , 2 , 3 ].each do |num| puts num end     Output   $ ruby block_with_each.rb 1 2 3 $    Blocks can also be saved in variables or passed as argument to another function.   yield is a Ruby keyword that is used to call a block. When you use the yield keyword, the code inside the block will run. Example of saving a bl