Learn Coding
57 subscribers
21 links
Download Telegram
πŸ“Œ Real Example β€” Simple Login Check

correct_password = "python123"

username = input("Enter username: ").strip().lower()
password = input("Enter password: ").strip()

if username == "admin" and password == correct_password:
print("Welcome back Admin!")
elif username == "admin" and password != correct_password:
print("Wrong password")
else:
print(f"User {username} not found")


Notice how we combined everything from the last 4 lectures
Variables, strings, input, f-strings, and now conditionals
This is how real programs are built β€” piece by piece
Please open Telegram to view this post
VIEW IN TELEGRAM
🚨 Video Reference

Watch this after reading through all the posts

Python Full Course 2024 β€” freeCodeCamp

πŸ”– Watch from 1:49:57 β†’ 2:38:25
Covers conditionals, if/elif/else, modulo operator, and even/odd logic
Please open Telegram to view this post
VIEW IN TELEGRAM
✏️ Lecture 4 Homework

Build a number guessing hint program:

secret = 42
guess = int(input("Guess the number: "))

if guess == secret:
print("Correct!")
elif guess > secret:
print(f"Too high β€” you were off by {guess - secret}")
else:
print(f"Too low β€” you were off by {secret - guess}")


This is the base β€” now make it your own
Change the number, add more messages, make it interesting
Screenshot your output

Bonus β€” add a check: if the guess is within 5 of the secret, print "So close!"

⚠️ Next lecture drops in 2 days β€” Loops
Please open Telegram to view this post
VIEW IN TELEGRAM
This media is not supported in your browser
VIEW IN TELEGRAM
Got exams this week...
lectures might get delayed a little
πŸ“š π—Ÿπ—²π—°π˜π˜‚π—Ώπ—² 𝟱 β€” π—Ÿπ—Όπ—Όπ—½π˜€

So far your code runs once and stops
What if you need to do something 100 times?
You are not going to write 100 lines
That is where loops come in

A loop runs a block of code over and over until you tell it to stop

This lecture covers:
➑️ while loops β€” repeat while something is true
➑️ for loops β€” repeat for each item in a sequence
➑️ range() β€” generating number sequences
➑️ break and continue β€” controlling your loops
➑️ Nested loops
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ While Loop

A while loop keeps running as long as a condition is true
The moment it becomes false β€” the loop stops

count = 1

while count <= 5:
print(f"Count: {count}")
count += 1

print("Done")


Output:


Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Done


count += 1 means count = count + 1
We increase count each time so the loop eventually stops

If you forget to update count the loop runs forever
This is called an infinite loop β€” press Ctrl+C to stop it
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ For Loop

A for loop repeats for each item in a sequence
It is cleaner and safer than while for most situations

fruits = ["apple", "banana", "mango"]

for fruit in fruits:
print(fruit)


Output:
apple
banana
mango


You can also loop over a string β€” it goes letter by letter:

for letter in "Python":
print(letter)
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ range()

range() generates a sequence of numbers for you to loop over

for i in range(5):
print(i)
# prints 0, 1, 2, 3, 4


```python
for i in range(1, 6):
print(i)
# prints 1, 2, 3, 4, 5


python
for i in range(0, 10, 2):
print(i)
# prints 0, 2, 4, 6, 8 (step of 2)`


range(start, stop, step)
Stop is always excluded β€” range(1, 6) gives you 1 to 5
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ break and continue

break β€” exits the loop immediately:

for i in range(10):
if i == 5:
break
print(i)
# prints 0, 1, 2, 3, 4 then stops


continue β€” skips the current iteration and moves to the next:

for i in range(10):
if i % 2 == 0:
continue
print(i)
# prints only odd numbers: 1, 3, 5, 7, 9


You will use break a lot in bots
For example β€” keep asking for input until the user types something valid
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ Real Example β€” Actual Number Guessing Game

Remember the homework from last lecture? Now we make it loop:

secret = 42
attempts = 0

while True:
guess = int(input("Guess the number: "))
attempts += 1

if guess == secret:
print(f"Correct! You got it in {attempts} attempts")
break
elif guess > secret:
print("Too high, try again")
else:
print("Too low, try again")


while True means loop forever
The only way out is the break when they guess correctly
This is a pattern you will see everywhere in real code
Please open Telegram to view this post
VIEW IN TELEGRAM
🚨 Video Reference

Watch this after reading through all the posts

Python Full Course 2024 β€” freeCodeCamp

πŸ”– Watch from 2:43:09 β†’ 3:31:22
Covers while loops, for loops, lists, and escape sequences
Please open Telegram to view this post
VIEW IN TELEGRAM
✏️ Lecture 5 Homework

Build a multiplication table generator:

number = int(input("Enter a number: "))

for i in range(1, 11):
print(f"{number} x {i} = {number * i}")


Run it with a few different numbers and screenshot the output

Bonus β€” wrap it in a while loop so after printing the table it asks
"Do you want another? (yes/no)" and keeps going until they say no

⚠️ Next lecture drops tomorrow β€” Lists & Tuples
Please open Telegram to view this post
VIEW IN TELEGRAM
This media is not supported in your browser
VIEW IN TELEGRAM
πŸ“š π—Ÿπ—²π—°π˜π˜‚π—Ώπ—² 𝟲 β€” π—Ÿπ—Άπ˜€π˜π˜€ & π—§π˜‚π—½π—Ήπ—²π˜€

Until now you stored one value in one variable
What if you need to store 100 values?
You are not making 100 variables
That is what lists are for

This lecture covers:
➑️ What lists are and how to create them
➑️ Accessing, updating, and deleting items
➑️ List methods
➑️ Looping through lists
➑️ Tuples β€” and when to use them instead
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“ŒWhat is a List?

A list is a collection of values stored in one variable
You create it with square brackets

fruits = ["apple", "banana", "mango"]
numbers = [1, 2, 3, 4, 5]
mixed = ["Ahmed", 22, True, 9.99] # lists can hold any types


Each item has an index β€” a position number starting from 0

fruits = ["apple", "banana", "mango"]

print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # mango
print(fruits[-1]) # mango β€” negative index counts from the end


This trips up beginners β€” the first item is index 0, not 1
Always remember β€” lists start at 0
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ Updating and Deleting Items

Updating an item:
fruits = ["apple", "banana", "mango"]
fruits[1] = "grape"
print(fruits) # ['apple', 'grape', 'mango']


Adding items:
fruits.append("orange")     # adds to the end
fruits.insert(1, "kiwi") # adds at index 1


Removing items:
fruits.remove("apple")   # removes by value
fruits.pop() # removes last item
fruits.pop(0) # removes item at index 0
del fruits[2] # deletes item at index 2


Checking if something is in a list:
if "mango" in fruits:
print("Found it")
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ Useful List Methods

numbers = [3, 1, 4, 1, 5, 9, 2, 6]

print(len(numbers)) # 8 β€” how many items
print(sorted(numbers)) # [1, 1, 2, 3, 4, 5, 6, 9] β€” sorted copy
print(numbers.count(1)) # 2 β€” how many times 1 appears
print(sum(numbers)) # 31 β€” adds all numbers
print(min(numbers)) # 1
print(max(numbers)) # 9

numbers.reverse() # reverses in place
numbers.sort() # sorts in place


Slicing β€” getting a portion of a list:
fruits = ["apple", "banana", "mango", "grape", "kiwi"]

print(fruits[1:3]) # ['banana', 'mango'] β€” index 1 to 2
print(fruits[:3]) # ['apple', 'banana', 'mango'] β€” from start to 2
print(fruits[2:]) # ['mango', 'grape', 'kiwi'] β€” from index 2 to end
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“ŒLooping Through Lists

This is where lists and loops combine β€” you will do this constantly

users = ["Ahmed", "Sara", "Ali", "Fatima"]

for user in users:
print(f"Hello {user}!")


If you also need the index:
for index, user in enumerate(users):
print(f"{index + 1}. {user}")


Output:
1. Ahmed
2. Sara
3. Ali
4. Fatima


enumerate() is incredibly useful β€” remember it
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“Œ Tuples

A tuple is exactly like a list but you cannot change it after creating it
You create it with round brackets instead of square

coordinates = (25.2048, 55.2708)  # latitude, longitude
rgb = (255, 0, 0) # red color


You can read from it the same way as a list:
print(coordinates[0])  # 25.2048


But you cannot change it:
coordinates[0] = 30  # ERROR β€” tuples are immutable


When to use a tuple vs a list:
➑️ Use a list when data will change β€” adding users, removing items
➑️ Use a tuple when data should never change β€” coordinates, colors, config
Please open Telegram to view this post
VIEW IN TELEGRAM