2. Python Basics

Variables in Python

What is a Variable?

A variable is used to store data in memory.

Example:

name = "Aditya"
age = 25

Here:

  • name stores text
  • age stores a number

Rules for Naming Variables

Valid Examples

student_name = "Rahul"
age2 = 20
_total = 500

Invalid Examples

2name = "Rahul"     # Cannot start with number
student-name = "A" # Hyphen not allowed

Multiple Variable Assignment

x, y, z = 10, 20, 30
print(x, y, z)

Dynamic Typing

Python automatically detects data type.

x = 10
x = "Hello"

Data Types in Python

Python has several built-in data types.


1. Integer (int)

Integers are whole numbers.

age = 25
marks = -90

print(type(age))

Output:

<class 'int'>

2. Float (float)

Float represents decimal numbers.

price = 99.99
pi = 3.14

Output:

<class 'float'>

3. String (str)

String stores text.

name = "Aditya"
city = 'Delhi'

String Examples

print(name)
print(city)

4. Boolean (bool)

Boolean has only two values:

  • True
  • False
is_logged_in = True
is_admin = False

5. Complex (complex)

Complex numbers contain real and imaginary parts.

x = 3 + 5j

print(type(x))

Output:

<class 'complex'>

Checking Data Type

Use type() function.

x = 10
print(type(x))

Type Conversion

Type conversion means changing one data type into another.


1. Integer to Float

x = 10
y = float(x)

print(y)

Output:

10.0

2. Float to Integer

x = 5.9
y = int(x)

print(y)

Output:

5

3. Integer to String

x = 100
y = str(x)

print(y)
print(type(y))

4. String to Integer

x = "50"
y = int(x)

print(y + 10)

Output:

60

Input and Output

Output using print()

print("Welcome to Python")

Taking Input from User

name = input("Enter your name: ")

print("Hello", name)

Input Always Returns String

age = input("Enter age: ")

print(type(age))

Output:

<class 'str'>

Convert Input to Integer

age = int(input("Enter age: "))

print(age + 5)

Operators in Python

Operators are used to perform operations on values and variables.


1. Arithmetic Operators

OperatorMeaningExample
+Addition5 + 2
-Subtraction5 - 2
*Multiplication5 * 2
/Division5 / 2
%Modulus5 % 2
**Power5 ** 2
//Floor Division5 // 2

Example

a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
print(a ** b)
print(a // b)

2. Comparison Operators

Used to compare values.

OperatorMeaning
==Equal
!=Not equal
>Greater than
<Less than
>=Greater than equal
<=Less than equal

Example

x = 10
y = 20

print(x == y)
print(x != y)
print(x > y)
print(x < y)

3. Logical Operators

Used with conditions.

OperatorMeaning
andBoth conditions true
orAny one condition true
notReverse condition

Example

age = 20

print(age > 18 and age < 30)
print(age > 18 or age < 10)
print(not(age > 18))

4. Assignment Operators

Used to assign values.

OperatorExample
=x = 5
+=x += 2
-=x -= 2
*=x *= 2
/=x /= 2

Example

x = 10

x += 5
print(x)

x *= 2
print(x)

5. Bitwise Operators

Operate on binary values.

OperatorMeaning
&AND
``
^XOR
~NOT
<<Left Shift
>>Right Shift

Example

a = 5
b = 3

print(a & b)
print(a | b)
print(a ^ b)

6. Membership Operators

Used to check membership in sequence.

OperatorMeaning
inPresent
not inNot present

Example

text = "Python"

print("P" in text)
print("z" not in text)

7. Identity Operators

Used to compare memory location.

OperatorMeaning
isSame object
is notDifferent object

Example

x = [1, 2]
y = x
z = [1, 2]

print(x is y)
print(x is z)

Output:

True
False