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 »

파이썬 연산과 함수

수식 Expression - 피연산자들과 연산자의 조합 피연산자 Operand: 연산의 대상 연산자 Operator: 어떤 연산을 나타내는 기호 예: +, -, , / 정확한 수식으로 표현된 연산을 프로그래밍 언어로 작성하면 컴퓨터가 정확히 계산합니다. 연산 우선 순위 - 소괄호 를...

제어 함수

조건문 Conditional Statement 조건에 따라 특정 동작을 하도록 하는 프로그래밍 명령어 명제 - 참 또는 거짓을 객관적이고 명확하게 판별할 수 있는 문장이나 식 관계 연산자 - x == y - x != y 논리 연산자 - X and Y - X or Y - not X IF...

파이썬 기초 (2)

자료형Data Type 1. 논리형bool: True1, False0 2. 정수형int: 1, 2, 3, … 3. 실수형float: 1.4, 5.33 4. 문자열str: 'hello', '안녕하세요' 그 외에도 다양 변수의 메모리 공간을 확보하는 행위가 실행 시점에 발생 변수에 값을...