#!/usr/bin/env python # coding: utf-8 # # The Zen of Python # In[1]: import this # # Variables # A name that is used to denote something or a value is called a variable. In python, variables can be declared and values can be assigned to it as follows, # In[2]: x = 2 y = 5 xy = 'Hey' # In[3]: print(x + y, xy) # Multiple variables can be assigned with the same value. # In[4]: x = y = 1 # In[5]: print(x, y) # # Operators # ## Arithmetic Operators # | Symbol | Task Performed | # |----|---| # | + | Addition | # | - | Subtraction | # | / | division | # | % | mod (modulo)| # | * | multiplication | # | // | floor division | # | ** | to the power of | # In[6]: 1 + 2 # In[7]: 2 - 1 # In[8]: 1 * 2 # In[9]: 5 / 3 # `//` if floor division also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int. The modulo operation `%` finds the remainder after division of one number by another (sometimes called modulus). # In[10]: 5 // 3 # In[11]: 5 // 3.0 # In[12]: a, b = 13, 3 b * (a // b) + (a % b) # ## Relational Operators # | Symbol | Task 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[13]: z = 1 # In[14]: z == 1 # In[15]: z > 1 # ## Bitwise Operators # | Symbol | Task Performed | # |----|---| # | & | Logical And | # | l | Logical OR | # | ^ | XOR | # | ~ | Negate | # | >> | Right shift | # | << | Left shift | # In[16]: a = 2 #0b10 b = 3 #0b11 # In[17]: print(a & b) print(bin(a & b)) # In[18]: 5 >> 1 # 0000 0101 -> 5 # # Shifting the digits by 1 to the right and zero padding # # 0000 0010 -> 2 # In[19]: 5 << 1 # 0000 0101 -> 5 # # Shifting the digits by 1 to the left and zero padding # # 0000 1010 -> 10 # # Built-in Functions # Python comes loaded with pre-built functions # ## Conversion from one 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 **0o** to the octal value or vice versa by using built in function **oct( )**. # In[20]: hex(170) # In[21]: 0xAA # In[22]: oct(8) # In[23]: 0o10 # **int( )** accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type. # In[24]: print(int('0o10',8)) print(int('0xaa',16)) print(int('1010',2)) # **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[25]: print(int(7.7)) print(int('7')) # Also note that function **bin( )** is used for binary and **float( )** for decimal/float values. **chr( )** is used for converting ASCII to its alphabet equivalent, **ord( )** is used for the other way round. # In[26]: chr(98) # In[27]: ord('b') # ## Simplifying Arithmetic Operations # **round( )** function rounds the input value to a specified number of places or to the nearest integer. # In[28]: print(round(5.6231)) print(round(4.55892, 2)) # **complex( )** is used to define a complex number and **abs( )** outputs the absolute value of the same. # In[29]: c = complex('3+4j') print(c, abs(c)) # **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[30]: divmod(9,2) # **isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once. # In[31]: print(isinstance(1, int)) print(isinstance(1.0,int)) print(isinstance(1.0,(int,float))) # **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[32]: print(pow(3,3)) print(pow(3,3,5)) # **`range(start, stop, step)`** 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. Function **`range`** generates an interator, which can be converted to a list using `list(...)` # In[33]: print(range(3), list(range(3))) print(range(2,9), list(range(2,9))) print(range(2,27,8), list(range(2,27,8))) # ## Accepting User Inputs # **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[37]: abc = input("Type something here and it will be stored in variable abc: ") # In[38]: type(abc) # Note that **type( )** returns the format or the type of a variable or a number