Skip to main content

Variables & Data types

In python variables are containers for storing data values, and data types define the type of data that a variable can hold. Python is dynamically typed, meaning you don't need to declare the type of a variable explicitly - the type is inferred at runtime.

1. Declaring variables​

Variables are created by assigning a value using the = operator.

x = 10         # Integer
y = 3.14 # Float
name = "Python" # String
is_active = True # Boolean

2. Naming rules​

  • Variable names must start with a letter or an underscore(_).
  • They can contain letters, numbers, and underscores but cannot start with a number.
  • Python is case-sensitive (age and Age are different variables).

3. Data types in python​

Data TypeDescriptionExample
intInteger numbersx = 10
floatFloating-point numbersy = 3.14
strStrings (text)name = "Python"
boolBoolean values (True or False)is_active = True
listOrdered, mutable collectionfruits = ["apple", "banana", "cherry"]
tupleOrdered, immutable collectioncoordinates = (10, 20)
dictKey-value pairs (dictionary)person = {"name": "John", "age": 25}
setUnordered, unique itemsunique_numbers = {1, 2, 3}
complexComplex numbersz = 1 + 2j
NoneTypeRepresents None (no value)value = None

4. Type casting​

Typecasting in Python refers to the process of converting one data type into another. Python provides built-in functions to perform this conversion explicitly. It is useful when you need to convert data between different types to perform operations or ensure compatibility.

Types of Typecasting​

  1. Implicit Typecasting

Python automatically converts one data type to another when it is safe to do so, without requiring explicit intervention.

x = 10   # Integer
y = 3.14 # Float
z = x + y # Python automatically converts `x` to float.
print(z) # Output: 13.14
print(type(z)) # Output: <class 'float'>
  1. Explicit Typecasting

This requires using Python's built-in functions to convert data types manually.

FunctionPurposeExample
int()Converts to an integerint("10") → 10
float()Converts to a floating-point numberfloat("3.14") → 3.14
str()Converts to a stringstr(10) → "10"
bool()Converts to a boolean (True/False)bool(0) → False
list()Converts to a listlist((1, 2, 3)) → [1, 2, 3]
tuple()Converts to a tupletuple([1, 2, 3]) → (1, 2, 3)
set()Converts to a setset([1, 1, 2]) → {1, 2}
dict()Converts to a dictionary (from key-value pairs)dict([(1, "a"), (2, "b")]) → {1: "a", 2: "b"}
complex()Converts to a complex numbercomplex(1, 2) → 1+2j

5. Operators​

CategoryOperatorDescriptionExample
Arithmetic+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
**Exponentiationa ** b
//Floor divisiona // b
Relational==Equal toa == b
!=Not equal toa != b
<Less thana < b
<=Less than or equal toa <= b
>Greater thana > b
>=Greater than or equal toa >= b
LogicalandLogical ANDa > 0 and b > 0
orLogical ORa > 0 or b > 0
notLogical NOTnot a
Bitwise&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << 1
>>Right shifta >> 1
Assignment=Assigna = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 5
/=Divide and assigna /= 5
%=Modulus and assigna %= 5
**=Exponentiation and assigna **= 5
//=Floor division and assigna //= 5
&=Bitwise AND and assigna &= b
`=`Bitwise OR and assign
^=Bitwise XOR and assigna ^= b
<<=Left shift and assigna <<= 1
>>=Right shift and assigna >>= 1
MembershipinReturns True if a value is found in a sequencex in [1, 2, 3]
not inReturns True if a value is not in a sequencex not in [1, 2, 3]
IdentityisReturns True if both variables refer to the same objecta is b
is notReturns True if variables refer to different objectsa is not b
Comparison<Less thana < b
<=Less than or equal toa <= b
>Greater thana > b
>=Greater than or equal toa >= b