Day 2 of Python (Operators and Conditional Statements)

Published: (February 5, 2026 at 02:21 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Operators

Operators are used to perform operations on values and variables.

Types of operators

Operators overview

Arithmetic operators

Used to perform basic mathematical operations like addition, subtraction, multiplication, etc.

Comparison operators

Used to compare values; they return True or False according to the given condition.

Logical operators

Used to perform logical AND, OR, and NOT operations. They combine conditional statements.

Assignment operators

Used to assign values to variables (the value on the right side of the expression is assigned to the left‑hand operand).

Formatted string (f‑string)

Allows inserting variables or values directly inside a string.

name = input("What is your name ")
print("Your name is ", name)
age = int(input("What is your age "))
print("Your age is ", age)
gender = input("What is your gender ")
print("Your gender is ", gender)
print(f"Your name is {name} age {age} gender {gender}")

Sample output

What is your name Vidya
Your name is  Vidya
What is your age 20
Your age is  20
What is your gender Female
Your gender is  Female
Your name is Vidya age 20 gender Female

By default, input from the user is stored as a string.

Typecasting

Converting from one data type to another.

  • Implicit typecasting – automatic conversion performed by Python.
  • Explicit typecasting – manual conversion using functions like int(), float(), str(), etc.

Strings

A sequence of characters used to represent data. Strings can be defined with single quotes (' '), double quotes (" "), or triple quotes (""" """) for multi‑line text.

Indexing

Accessing individual elements using their position (index) in a sequence.

Negative indexing

Accessing elements from the end of a sequence using negative numbers.

Slicing

Extracting a part of a sequence (e.g., a string or list) by specifying start and end indices.

Conditional Statements

Conditional statements allow a program to make decisions based on given conditions.

Types of conditional statements

  • if statement – Executes code only if the condition is true.
  • if‑else statement – Provides an alternative block of code when the condition is false.
  • elif statement – Handles multiple mutually exclusive conditions.
Back to Blog

Related posts

Read more »

Operators

What are Operators in Java? Operators are symbols used to perform operations on variables and values. Types of Operators in Java - Arithmetic Operators - Assig...