Variable Operators and Built-in Functions in Python

 

Video Tutorial

Variable Operators and Built-in Functions in Python

The objective of this tutorial is to understand basic concepts of python programing language such as Variable Operators and Built-in Functions in Python,

  • Variables
  • Operators
    • Arithmatic Operators
    • Logical Operators
    • Bit-Wise Operators
  • Built-in Functions

Variables

A name that is used to denote something or a some value is called a variable. The python is loosely-typed programming language. That is, at the time of declaring or defining a variable, there is no need to mention the type of variable. The type of variable is determined by the valued assigned to it. In python, variables can be declared and values can be assigned to it as follows,

In [1]:
x = 2
y = 5.5
xy = 'Hey'
In [2]:
print (x)
print (y)
print (x+y, xy)
2
5.5
7.5 Hey

In python the same value can be assigned to multiple variables with a single statement. Also, different values to multiple variables can be assigned with the single statement.

In [3]:
x = y = 10
print ('x =', x, 'y =', y)

a, b = 10, 20
print ('a =',a, 'b =', b)
x = 10 y = 10
a = 10 b = 20

Operators

Arithmatic Operators

The following are the arithmatic operators supported by python programming language.

SymbolTask Performed
+Addition
Subtraction
/division
%mod
*multiplication
//floor division
**to the power of
In [4]:
print (1+2)
print (2-1)
print (5*2)
print (15%4)
print (5/2)
print (5//2)
3
1
10
3
2.5
2

Last answer 2 … How? This is because // operator is floor division operator both the numerator and denominator are integers but the result is a float value hence an integer value is returned. By changing either the numerator or the denominator to float, correct answer can be obtained. In the next example numerator is integer but denominator is float, hence the result is interger converted float value. Floor division is nothing but converting the result so obtained to the nearest integer.

In [5]:
5//2.0
2.0

Relational Operators

Relational operators are used to compare the relationship between two variable. The following are the list of realtional operators in python.

SymbolTask Performed
==True, if it is equal
!=True, if not equal to
<less than
>greater than
<=less than or equal to
>=greater than or equal to
In [6]:
z = 1
In [7]:
z == 1
True
In [8]:
z > 1
False

Bitwise Operators

SymbolTask Performed
&Logical And
lLogical OR
^XOR
~Negate
>>Right shift
<<Left shift
In [9]:
a = 2 #10 binary equivalant  of 3 # is used to add comment lines
b = 3 #11 binary equivalant of 3
In [10]:
print (a & b)
2
In [11]:
5 >> 1   # >> is Right shipt operator
2

0000 0101 -> 5

Shifting the digits by 1 to the right and zero padding

0000 0010 -> 2

In [12]:
5 << 1  #<<> is left shipt operator
10

0000 0101 -> 5

Shifting the digits by 1 to the left and zero padding

0000 1010 -> 10

Built-in Functions Python

Python comes loaded with pre-built functions

Conversion from one base system to another

Conversion from hexadecimal to decimal is done by adding prefix 0x to the hexadecimal value or vice versa by using built in hex( ), Octal to decimal by adding prefix 0 to the octal value or vice versa by using built in function oct( ).

In [13]:
print (hex(170))
print (0xAA)
print (oct(8))
print (0o10)
0xaa
170
0o10
8

int( ) accepts two papmeters when used for conversion, first parameter is the value in a different number system and the second parameter is its base. Note that input number in the different number system should be of string type.

In [14]:
print (int('012',8))
print (int('aa',16))
print (int('1010',2))
10
170
10

int( ) can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function str( ) can be used to convert the integer back to string format

In [15]:
print (int(7.7))
print (int('7'))
7
7

Also note that function bin( ) function is used to convert to binary conversion and float( ) for decimal/float values. chr( ) is used for converting ASCII to its alphabet equivalent, ord( ) is used for the other way round. Similarly, hex is used convert number to hexadecimal format.

In [16]:
print (chr(98))
print (bin(8))
print (ord('b'))
print (hex(16))
b
0b1000
98
0x10

Simplifying Arithmetic Operations

round( ) function rounds the input value to a specified number of places or to the nearest integer.

In [17]:
print (round(5.6231)) 
print (round(4.55892, 2))
6
4.56

complex( ) is used to define a complex number and abs( ) outputs the absolute value of the same.

In [18]:
c =complex('5+2j')

#abs value of a+bj is sqrt(a^2+b^2)

print (abs(c))
5.385164807134504

divmod(x,y) outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder).

In [19]:
divmod(9,2)
(4, 1)

isinstance( ) returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once.

In [20]:
print (isinstance(1, int))
print (isinstance(1.0,int))
print (isinstance(1.0,(int,float)))
True
False
True

pow(x,y,z) can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z).

In [21]:
print (pow(3,3))
print (pow(3,3,5))
27
2

range( ) function outputs the integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are returned in a list (will be discussing in detail later.)

In [22]:
print (range(3))
print (range(2,9))
print (range(2,27,8))
range(0, 3)
range(2, 9)
range(2, 27, 8)

Accepting User Inputs

raw_input( ) accepts input and stores it as a string. Hence, if the user inputs a integer, the code should convert the string to an integer and then proceed.

In [23]:
x = input()
10
In [24]:
x = input("Enter a number\t")
Enter a number	10
In [25]:
type(x)
str

Note that type( ) returns the format or the type of a variable or a number

Summary

This tutorial discusses the basic concepts of python programing language such as Variable Operators and Built-in Functions in Python. If you like the tutorial share with your friends.

Leave a Comment

Your email address will not be published. Required fields are marked *