SortedCoding
1.27K subscribers
185 photos
38 videos
194 files
141 links
Learn to code with clarity and precision
Download Telegram
❀1
Which syntax creates an empty list?
Anonymous Quiz
58%
[]
15%
{}
18%
()
9%
set()
❀1
Today, let's move on to the next topic in the Python Coding Challenge:

Dictionaries & Sets πŸ”‘πŸ§Ί

πŸ”ΉDay 5: What is a Dictionary in Python?

A dictionary is an unordered, mutable collection that stores data in key-value pairs.

🧠 Example:

student = {
"name": "Amit",
"age": 21,
"course": "Python"
}

print(student["name"]) # Output: Amit

- Keys must be unique and immutable (like strings, numbers).
- Values can be anything: strings, numbers, lists, or even other dictionaries.


🧰 Common Dictionary Methods:

student.get("age") # Returns 21
student.keys() # Returns all keys
student.values() # Returns all values
student.items() # Returns key-value pairs
student["grade"] = "A+" # Adds a new key-value pair


πŸ”Ή What is a Set in Python?

A set is an unordered collection of unique elements.

🧠 Example :

numbers = {1, 2, 3, 4, 4, 2}
print(numbers) # Output: {1, 2, 3, 4} β€” no duplicates

- Sets remove duplicates automatically.
- Useful for membership checks, uniqueness, and set operations (union, intersection).


βœ… Real-Life Project: Contact Book using Dictionary

- Build a CLI-based contact book where users can:
- Add new contacts (name, phone)
- View all contacts
- Search by name
- Delete a contact


πŸ’‘ Python Code:

contacts = {}

while True:
print("\n1. Add Contact\n2. View All\n3. Search\n4. Delete\n5. Exit")
choice = input("Enter choice: ")

if choice == '1':
name = input("Name: ")
phone = input("Phone: ")
contacts[name] = phone
print("Contact saved!")

elif choice == '2':
for name, phone in contacts.items():
print(f"{name} : {phone}")

elif choice == '3':
name = input("Enter name to search: ")
if name in contacts:
print(f"{name}'s phone: {contacts[name]}")
else:
print("Contact not found.")

elif choice == '4':
name = input("Enter name to delete: ")
if name in contacts:
del contacts[name]
print("Deleted successfully.")
else:
print("No such contact.")

elif choice == '5':
break
else:
print("Invalid choice.")


React with ❀️ once you’re ready for the quiz
❀2
What will be the output of following code?

d = {"a": 1, "b": 2} print(d.get("c"))
Anonymous Quiz
10%
0
29%
Error
27%
None
34%
"c"
❀1
Which one of these is NOT allowed as a key in a dictionary?
Anonymous Quiz
21%
integer
17%
String
48%
Tuple
14%
List
❀2
Which method returns all key-value pairs in a dictionary?
Anonymous Quiz
32%
.Values()
17%
.get()
27%
.items()
24%
.keys()
❀1
What will this code output?

info = {"name": "Alex", "age": 25} info["city"] = "New York" print("info")
Anonymous Quiz
44%
{'name': 'Alex', 'age': 25, 'city': 'New York'}
21%
{'city': 'New York'}
17%
info
19%
Error
❀1
What will be the output of this code ?
s = {1,2,3,2,4}
print(len(s))
Anonymous Quiz
62%
5
27%
4
9%
3
2%
2
❀2
Today, Let’s move on to the next topic in the Python Coding Challenge:

πŸ”ΉDay 6: Conditionals (if, elif, else)

In Python, conditional statements allow your code to make decisions.

πŸ’‘ What Are Conditionals?

They help your program execute certain code blocks only when specific conditions are true.

βœ… Syntax :

if condition:
# Code runs if condition is True
elif another_condition:
# Runs if previous conditions were False, this one is True
else:
# Runs if none of the above conditions are True

🧠 Example :

age = 18

if age >= 18:
print("You’re an adult.")
elif age > 13:
print("You’re a teenager.")
else:
print("You’re a child.")

Output:

You’re an adult.


🎯 Mini Project: Guess the Number Game

Let’s build a small game using what we’ve learned so far:

Python Code

import random

number = random.randint(1, 10)
guess = int(input("Guess a number between 1 and 10: "))

if guess == number:
print("πŸŽ‰ Correct! You guessed it right.")
elif guess < number:
print("Too low! Try again.")
else:
print("Too high! Try again.")

print(f"The correct number was: {number}")


This project uses:

- if, elif, else
- User input
- Random module


React with ❀️ once you’re ready for the quiz
❀6
Today, let's start with the first topic in Python Programming Roadmap:

βœ… Python Programming Basics πŸπŸ’»

πŸ“Œ Step 1: Install Python & VS Code
* Download Python 3.11+ from python.org
* During install, check βœ… Add Python to PATH
* Install VS Code
* In VS Code, install the Python extension
To check:
Open terminal β†’ Type: python --version
You should see something like: Python 3.x.x

πŸ“Œ Step 2: Your First Python Program
Create a file: hello.py
Paste this code:

print("Hello, Python")

Run it in terminal: python hello.py
🧠 Python runs code top to bottom
πŸ–¨ print() displays output on the screen

πŸ“Œ Step 3: Variables
Variables store values.

age = 25
name = "Deepak"
height = 5.11
print(age, name, height)

βœ”οΈ No need to declare types β€” Python figures it out
βœ”οΈ Use lowercase names with underscores

πŸ“Œ Step 4: Data Types
* int β†’ Whole numbers (e.g., 10)
* float β†’ Decimals (e.g., 3.14)
* str β†’ Text (e.g., "hello")
* bool β†’ True / False

To check type:

x = 10
print(type(x))


πŸ“Œ Step 5: Input & Output
Take input from user:
python
name = input("Enter your name: ")
print("Hello", name)


Convert string input to number:

age = int(input("Enter age: "))
print("Next year you'll be", age + 1)


πŸ“Œ Step 6: Arithmetic Operators

a = 10
b = 3
print(a + b) # Add
print(a - b) # Subtract
print(a * b) # Multiply
print(a / b) # Divide
print(a // b) # Floor division
print(a % b) # Remainder


πŸ“Œ Step 7: String Operations

first = "Data"
second = "Analyst"
print(first + " " + second) # Concatenate
print("Hi " * 3) # Repeat
print(len(first)) # Length


πŸ“Œ Step 8: Practice Programs
1️⃣ Simple Calculator
β†’ Input 2 numbers, show sum, difference, product, division

2️⃣ Temperature Converter
β†’ Input Celsius, convert to Fahrenheit
F = (C * 9/5) + 32

3️⃣ Age After 5 Years
β†’ Input current age, print age after 5 years


Here is the detailed code for each project:

πŸ“Œ Project 1. Simple Calculator

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)

if num2 != 0:
print("Division:", num1 / num2)
else:
print("Division not possible")


πŸ“Œ Project 2. Temperature Converter (Celsius to Fahrenheit)

celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9 / 5) + 32
print("Temperature in Fahrenheit:", fahrenheit)


πŸ“Œ Project 3. Age After 5 Years

age = int(input("Enter your age: "))
future_age = age + 5
print("Your age after 5 years:", future_age)


🎁 Bonus Practice – Area of a Rectangle

length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area of rectangle:", area)


πŸ’‘Useful Tips
* Run each program
* Change input values
* Break it on purpose and fix it

🧠 Daily Rule:
* Code at least 60 mins
* Type every line manually
* Don’t copy-paste β€” build muscle memory

I have decided to give some quizzes after this post to test your knowledge
❀2
What is the output of this code?
print(10 // 3)
Anonymous Quiz
27%
3.33
39%
3
18%
4
15%
3.0
❀1
Which operator is used for string repetition?
Anonymous Quiz
29%
+
56%
*
6%
&
9%
%
❀2
Now, let's move to the next topic in Python Programming Roadmap:

βœ… Python Control Flow Part 1: if, elif, else πŸ§ πŸ’»

What is Control Flow?
πŸ‘‰ Your code makes decisions
πŸ‘‰ Runs only when conditions are met

* Each condition is True or False
* Python checks from top to bottom

πŸ”Ή Basic if statement
python
age = 20
if age >= 18:
print("You are eligible to vote")

▢️ Checks if age is 18 or more. Prints "You are eligible to vote"

πŸ”Ή if-else example
python
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible")

▢️ Age is 16, so it prints "Not eligible"

πŸ”Ή elif for multiple conditions
python
marks = 72
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Fail")

▢️ Marks = 72, so it matches >= 60 and prints "Grade C"

πŸ”Ή Comparison Operators
python
a = 10
b = 20
if a != b:
print("Values are different")

▢️ Since 10 β‰  20, it prints "Values are different"

πŸ”Ή Logical Operators
python
age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed")

▢️ Both conditions are True β†’ prints "Entry allowed"

⚠️ Common Mistakes:
* Using = instead of ==
* Bad indentation
* Comparing incompatible data types

πŸ“Œ Mini Project – Age Category Checker
python
age = int(input("Enter age: "))

if age < 13:
print("Child")
elif age <= 19:
print("Teen")
else:
print("Adult")

▢️ Takes age as input and prints the category


πŸ“ Practice Tasks:
1. Check if a number is even or odd
2. Check if number is +ve, -ve, or 0
3. Print the larger of two numbers
4. Check if a year is leap year

βœ… Practice Task Solutions – Try it yourself first πŸ‘‡

1️⃣ Check if a number is even or odd
python
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")

▢️ % gives remainder. If remainder is 0, it's even.


2️⃣ Check if number is positive, negative, or zero
python
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")

▢️ Uses > and < to check sign of number.


3️⃣ Print the larger of two numbers
python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
print("Larger number is:", a)
elif b > a:
print("Larger number is:", b)
else:
print("Both are equal")

▢️ Compares a and b and prints the larger one.


4️⃣ Check if a year is leap year
python
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")

▢️ Follows leap year rules:
- Divisible by 4 βœ…
- But not divisible by 100 ❌
- Unless also divisible by 400 βœ…


πŸ“… Daily Rule:
βœ… Code 60 mins
βœ… Run every example
βœ… Change inputs and observe output
❀3
What will this code print?

x = 15 if x> 10: print("A") elif x> 5: print("B") else: print("C")
Anonymous Quiz
67%
A
24%
B
9%
C
❀3
Which operator checks if two values are equal?
Anonymous Quiz
17%
=
69%
==
7%
!=
7%
>=
❀3
What is the output of this code?

a = 5 b = 10 if a> b: print("a is greater") else: print("b is greater")
Anonymous Quiz
9%
a is greater
70%
b is greater
13%
Error
9%
nothing
❀4
Which of the following is a correct way to check if a number is divisible by both 3 and 5?
Anonymous Quiz
22%
`if num % 3 and num % 5 == 0:`
20%
`if num % 3 == 0 or num % 5 == 0:`
48%
`if num % 3 == 0 and num % 5 == 0:`
10%
`if num == 3 and 5:`
❀3
What is the mistake in this code?

age = 17 if age>= 18 print("Adult")
Anonymous Quiz
29%
Indentation error
36%
Missing colon after `if`
19%
`age` should be a string
16%
No error
❀4