Datatypes and Operators in Python
**Datatypes**
Datatypes is a classification of data which tells the compiler or interpreter how the programmer intends to use the data.
* Numeric Types: int, float,complex.
* Boolean Type: bool
* Dictionary
* set
* sequence:str,list,tuple
**Numeric Types**
Numeric types are represented the data by numeric values.
Numeric values are in integer,float,complex
**Integer** - is the whole number ,no decimal values in it(eg.99,3)
**float** - is the decimal values(eg.4.6,3.608)
**complex** - which has both real(7) and imaginery number (7j)(eg.7+7j)
**Boolean**
Two build in values true(1) and false(0)
**set**
It is the unordered collection of datatypes.it is iterable(looping),mutable(changeable) and has no duplicate elements(unique). (eg.x=set("gobi"))
**sequence**
It is the ordered collection of similar or different datatypes.it allows to store multiple values
**-_string_** -collection of characters enclosed with single('),double("),triple("') quotes (eg. name='gobi',name="rahul",name="'ram"')
**_-list_** -list is like array.collection of elements enclosed within square brackets[] .(eg.a=[1,2,4],b=["janu","sonu"])
**_-tuple_** -tuple is like the list.collection of elements enclosed with parathesis.(eg.a=(1,2,4),b=("janu","sonu"))
**Dictionary**
It is the unordered collection of data in key-value pair.it is enclosed with curly brackets{} (eg.eg.a={1:1,2:2,3:4},b={1:"janu",2:"sonu"})
# --- 1. Numeric Types ---
# Integer (int)
age = 99
count = 3
# Float (float)
price = 4.6
pi_approx = 3.608
# Complex (complex)
z = 7 + 7j
# --- 2. Boolean Type ---
# Boolean (bool)
is_active = True
is_logged_in = False
# --- 3. Set ---
# Set (set) - unordered, mutable, no duplicates
letters = set("gobi")
numbers = {1, 2, 3, 3} # Duplicate 3 is automatically removed
# --- 4. Sequence Types ---
# String (str)
name1 = 'gobi'
name2 = "rahul"
name3 = """ram"""
# List (list) - ordered, mutable
list_nums = [1, 2, 4]
list_names = ["janu", "sonu"]
# Tuple (tuple) - ordered, immutable
tuple_nums = (1, 2, 4)
tuple_names = ("janu", "sonu")
# --- 5. Dictionary ---
# Dictionary (dict) - key-value pairs
dict_nums = {1: 1, 2: 2, 3: 4}
dict_names = {1: "janu", 2: "sonu"}
**Comments**
which are not execute in the code
**single line comments** -start with **hastag(#)**
**multiple line comments** -python doesn't provide option for multiple line comments .using multiple hastag to write multiple line comments
# This is a single line comment
x = 10 # This is also a single line comment
# This is a multiple line comment
# written using multiple hashtags
# across different lines
y = 20
print(x + y)
**OPERATORS**
Which used to do some operations like addition,assigning ,division etc with the help of operators
**Types of operators:**
* Arithmetic Operators-Mathematical calculations (+, -, _, /, %, //, *_)
* Comparison Operators or relational operators-Compare two values (==, !=, , , =, =)
* Assignment Operators-Assign and update values (=, +=, -=)
* Logical Operators-Combine conditions (and, or, not)
* Bitwise Operators-Perform binary operations (&, |, ^, ~, , )
* Membership Operators-Check whether an element exists (in, not in)
* Identity Operators-Check object identity (is, is not)
**1. Arithmetic Operators**
Arithmetic operators are used to perform mathematical calculations.
* + Addition-->10 + 5 15
* - Subtraction-->10 - 5 5
* * Multiplication-->10 * 5 50
* / Division-->10 / 5 2.0 - / returns a floating-point result.
* % Modulus (remainder)-->10 % 3 1 - % returns the remainder.
* // Floor Division-->10 // 3 3 - // returns the floor of the division result.
* ** Exponentiation-->2 ** 3 8 - ** calculates the power.
a = 10
b = 3
print(a + b) #13
print(a - b) #7
print(a * b) #30
print(a / b) #3.3
print(a % b) #1
print(a // b) #3
print(a ** b) #1000
**2. Comparison Operators (Relational Operators)**
Comparison operators are used to compare two values.
They return either True or False.
* == Equal to 10 == 10 True
* != Not equal to 10 != 5 True
* > Greater than 10 > 5 True
* < Less than 10 < 5 False
* >= Greater than or equal to 10 >= 10 True
* <= Less than or equal to 5 <= 10 True
a = 20
b = 10
print(a == b) # False
print(a != b) # True
print(a > b) # True
print(a < b) # False
print(a >= b) # True
print(a <= b) # False
**3. Assignment Operators**
Assignment operators are used to assign values to variables or update their values.
* = a = 10-->Assign 10
* += a += 5-->a = a + 5
* -= a -= 5-->a = a - 5
* *= a *= 5-->a = a * 5
* /= a /= 5-->a = a / 5
* %= a %= 3-->a = a % 3
* //= a //= 3-->a = a // 3
* *_= a *_ = 2-->a = a ** 2
a = 10
a += 5
print(a) # 15
a -= 3
print(a) # 12
a *= 2
print(a) # 24
**4. Logical Operators**
Logical operators are used to combine multiple conditions.
Python has three logical operators:
* && and True if both conditions are True
* || or True if at least one condition is True
* ! not Reverses the result
a = 10
b = 20
print(a > 5 && b > 10) # True
print(a > 15 || b > 10) # True
print(not(a ! 5)) # False
Truth table:
A B A and B A or B
True True True True
True False False True
False True False True
False False False False
**5. Bitwise Operators**
Bitwise operators perform operations on the binary (0s and 1s) representation of integers.
Operator | Name | Example
---|---|---
& | AND | 5 & 3
| | OR
^ | XOR | 5 ^ 3
~ | NOT | ~5
<< | Left Shift | 5 << 1
>> | Right Shift | 5 >> 1
a = 5
b = 3
print(a & b) # 1
print(a | b) # 7
print(a ^ b) # 6
print(~a) # -6
print(a << 1) # 10
print(a >> 1) # 2
_Binary explanation:_
* 5 = 0101
* 3 = 0011 For AND: 0101 & 0011 #0001 = 1
**6. Membership Operators**
Membership operators check whether a value exists in a sequence such as a string, list, tuple or set.
* in Checks whether an element exists
* not in Checks whether an element does not exist
fruits = ["apple", "banana", "orange"]
print("apple" in fruits) # True
print("mango" in fruits) # False
print("mango" not in fruits) # True
**7. Identity Operators**
Identity operators check whether two variables refer to the same object in memory.
* is Both variables refer to the same object
* is not Variables refer to different objects
a = [10, 20, 30]
b = a
c = [10, 20, 30]
print(a is b) # True
print(a is c) # False
print(a == c) # True
print(a is not c) # True
**8.Ternary operator**
The ternary operator in Python (officially called a conditional expression) is a compact way to write an if-else statement in a single line of code.
result = [value_if_true] if [condition] else [value_if_false]
age = 20
# Reads as: Set status to "Adult" IF age >= 18, ELSE set it to "Minor"
status = "Adult" if age >= 18 else "Minor"
print(status) # Output: Adult