Python


Comments

# Single line comment or Inline comment

""" 
    Multiline comment 
"""

Get Input

var = input("Enter sth: ")

Input method always returns a string, hence need a typecast to appropriate data-type.

var = int(input("Enter sth: "))

Display Output

print("Hello World");

Data-types in Python

Type Denotion
String str
Integer int
Float float
Boolean bool
Complex complex
  • We can use type() method to find the data type of a variable.
  • We cannot convert complex numbers into another data type.

Operator’s

Arithmetic Operators
Name Denotion
Addition
+
Sutraction
-
Multiplication
*
Division
/
Modulus
%
Exponenation
**
Floor Division
//
Comparison Operators
Name Denotion
Equal
==
Not Equal
!=
Greater than
>
Less than
<
Greater than or equal to
>=
Less than or equal to
<=
Logical Operators
Name Description
and
Returns True if both statements are true
or
Returns True if one of the statements is true
not
Reverse the result, returns False if the result is true
Identity Operators
Name Description
is
Returns True if both variables are the same object
is not
Returns True if both variables are not the same object
Membership Operators
Name Description
in
Returns True if a sequence with the specified value is present in the object
not in
Returns True if a sequence with the specified value is not present in the object
Bitwise Operators
Name Denotion
AND
&
OR
|
XOR
^
NOT
!
Left Shift
<<
Right Shift
>>
Assignment Operators
Name Example
=
x = 5
+=
x += 5
-=
x -= 5
*=
x *= 3
/=
x /= 3
%=
x %= 3
//=
x //= 3
**=
x **= 2
&=
x &= 2
|=
x |= 2
^=
x ^= 2
>>=
x >>= 4
<<=
x <<= 4

Conditional Statement

if <condition>:
  pass

elif <condition>:
  pass
 
else:
  pass

Loops

For loop:

for i in range(n):
  pass

While loop:

while <condition>:
  pass

Note: Python does not support do while loop.

Basic data Structures

List
  • Ordered collection of heterogeneous data types.
  • Ordered means every element has an index(subscript) associated to it.
  • Denotion:- [ ]
  • List can have duplicate elements.
  • List is mutatable, that is element(s) of list can be modified.
Tuple
  • Ordered collection of heterogeneous data types.
  • Ordered means every element has an index(subscript) associated to it.
  • Denotion:- ( )
  • Tuple can have duplicate elements.
  • Tuple is immutatable, that is element(s) of tuple cannot be modified.
Set
  • Unordered collection of heterogeneous data types.
  • Unordered means element(s)don't have an index(subscript) associated to it.
  • Denotion:- { }
  • Set cannot have duplicate elements.
  • Set is mutatable, that is element(s) of tuple can be modified.
Dictionary
  • Ordered collection of Key-Value pair.
  • Ordered means dictionary remembers the order in which elements get inserted into the dictionary.
  • Denotion:- { }
  • Dictionary cannot have duplicate Keys.
  • Dictionary is mutatable, that is values associated to key(s) can be modified.

Functions

Declaration:

def <function_name>(<input arguements>):
  pass

For example:

def add(a, b):
  return a+b

Function declaration with data type of input argument(s) and return variable:

def add(a:int, b:float)->int:
  return a+b