Introduction To Python
Introduction To Python
Python Data types are the classification or categorization of data items. It represents
the kind of value that tells what operations can be performed on a particular data.
Since everything is an object in Python programming, Python data types are classes
and variables are instances (objects) of these classes. The following are the standard
or built-in data types in Python:
Numeric
Sequence Type
Boolean
Set
Dictionary
Binary Types
Python has two built-in methods that you can use on tuples.
Method Description
index() Searches the tuple for a specified value and returns the position of where it
was found
Python Operators
+ Addition a + b = 30
- Subtraction a – b = -10
* Multiplication a * b = 200
/ Division b/a=2
% Modulus b%a=0
Comparison operators compare the values on either side of them and decide the
relation among them. They are also called Relational operators.
= a = 10 a = 10
+= a += 30 a = a + 30
-= a -= 15 a = a - 15
*= a *= 10 a = a * 10
/= a /= 5 a=a/5
%= a %= 5 a=a%5
**= a **= 4 a = a ** 4
//= a //= 5 a = a // 5
|= a |= 5 a=a|5
^= a ^= 5 a=a^5
Bitwise operator works on bits and performs bit by bit operation. These
operators are used to compare binary numbers.
| OR a|b
^ XOR a^b
~ NOT ~a
Python logical operators are used to combile two or more conditions and check
the final result. There are following logical operators supported by Python
language. Assume variable a holds 10 and variable b holds 20 then
or OR a or b
returns True if it does not finds a variable in the specified sequence and false a not in
not in
otherwise. b
Identity operators compare the memory locations of two objects. There are two
Identity operators explained below −
is Returns True if both variables are the same object and false otherwise. a is b
is not Returns True if both variables are not the same object and false otherwise. a is not b
Python Variables
Instance methods
Class methods
Static methods
Instance methods
Instance methods are the most used methods in a Python class. These methods are only accessible through
class objects. If we want to modify any class variable, this should be done inside an instance method.
Class methods
Class methods are usually used to access class variables. You can call these methods directly using the class
name instead of creating an object of that class.
To declare a class method, we need to use the @classmethod decorator. Also, as in the case of instance
methods, self is the keyword used to access the class variables. In class methods, we use use
the cls variable to refer to the class.
Static methods
Static methods are usually used as a utility function or when we do not want an inherited class to modify a
function definition. These methods do not have any relation to the class variables and instance variables; so,
are not allowed to modify the class attributes inside a static method.
To declare a static method, we need to use the @staticmethod. Again, we will be using the cls variable to
refer to the class. These methods can be accessed using the class name as well as class objects.