PYTHON

Variables and Data Types


Datatype derives type of data had ben stored in a variable.

  • In python, interpreter recognizes datatype completely while initialization only. 
  • Hence, no need to define type of data every time whenever we initialize variable.
  • There are mainly two types and totally 9 types of datatype in python.

 

Numeric datatypes in Python:

Python provides several built-in numeric data types to handle various kinds of numerical operations. The primary numeric data types in Python are:

  1. Integer ('int')
  2. Floating ('float')
  3. Complex ('complex')

 

Integers:

The whole numbers(positive, negative, zero) which lay on number line are called integers.

  • Its a individual, non-iterable type.
  • Its immutable type(cannot modify value without using “=”).
  • Default value : zero.

Example of Integer datatype:

a = 10
b = -5

 

Basic Arithmetic Operations:

x = 10
y = 3
# Addition
print(x + y)  # Output: 13
# Subtraction
print(x - y)  # Output: 7
# Multiplication
print(x * y)  # Output: 30
# Division
print(x / y)  # Output: 3.3333333333333335 (float division)
# Floor Division
print(x // y)  # Output: 3 (integer division)
# Modulus
print(x % y)  # Output: 1
# Exponentiation
print(x ** y)  # Output: 1000

 

Built-in Functions for Integers:

Python provides several built-in functions that work with integers.

Examples:

# Absolute value
print(abs(-10))  # Output: 10
# Power
print(pow(2, 3))  # Output: 8
# Maximum
print(max(1, 5, 3))  # Output: 5
# Minimum
print(min(1, 5, 3))  # Output: 1
# Sum
print(sum([1, 2, 3, 4, 5]))  # Output: 15
# Convert string to integer
print(int("123"))  # Output: 123
# Convert float to integer
print(int(9.99))  # Output: 9
 

Type Conversion:

You can convert other data types to integers using the int() function.

Examples:

# String to integer
s = "123"
n = int(s)
print(n)  # Output: 123
# Float to integer
f = 3.14
n = int(f)
print(n)  # Output: 3

 

 

Float:

It is a type which includes all decimal numbers. [ . ] is used to symbolize decimal point.

  • It's a numeric, non-seperatable, single and non-traversable data type.
  • It's a immutable type.
  • Default value is 0.0 .

Example of Float datatype:

p = 3.14
g = 9.8

 

Float Operations:

Python supports various operations on floats.

Basic Arithmetic Operations:

x = 7.5
y = 2.3
# Addition
print(x + y)  # Output: 9.8
# Subtraction
print(x - y)  # Output: 5.2
# Multiplication
print(x * y)  # Output: 17.25
# Division
print(x / y)  # Output: 3.260869565217391
# Floor Division
print(x // y)  # Output: 3.0 (float division, returns float)
# Modulus
print(x % y)  # Output: 0.5999999999999996
# Exponentiation
print(x ** y)  # Output: 1793.1721886045393

 

Type Conversion:

You can convert other data types to floats using the float() function.

Examples:

# Integer to float
a = 10
b = float(a)
print(b)       # Output: 10.0
print(type(b)) # Output: <class 'float'>
# String to float
s = "3.14"
c = float(s)
print(c)       # Output: 3.14
print(type(c)) # Output: <class 'float'>

 

 

Complex:

The numbers which are not present on number line is called imaginary number.

  • The number which are existing in real number line are called real numbers.
  • Combination of real and imaginary number is called complex number.
  • Complex number are represent in “a + bj” format, where a and b are real numbers and j is imaginary number. 

Example of Complex datatype:

c = 2 + 3j
d = complex(1, 2)

 

Accessing Real and Imaginary Parts:

You can access the real and imaginary parts of a complex number using the .real and .imag attributes.

Examples:

z = 3 + 4j
real_part = z.real
imaginary_part = z.imag
print(real_part)       # Output: 3.0
print(imaginary_part)  # Output: 4.0

Arithmetic Operations:

Python supports various arithmetic operations on complex numbers.

Examples:

z1 = 3 + 4j
z2 = 1 - 2j
# Addition
print(z1 + z2)  # Output: (4+2j)
# Subtraction
print(z1 - z2)  # Output: (2+6j)
# Multiplication
print(z1 * z2)  # Output: (11-2j)
# Division
print(z1 / z2)  # Output: (-1.4+1.8j)

 

Type Conversion:

You can convert other numeric types to complex using the complex() function.

Examples:

# Integer to complex
a = 10
z = complex(a)
print(z)       # Output: (10+0j)
print(type(z)) # Output: <class 'complex'>
# Float to complex
f = 3.14
z = complex(f)
print(z)       # Output: (3.14+0j)
print(type(z)) # Output: <class 'complex'>

 

 


PYTHON