Baskartech Academy Python
Baskartech Academy Python
ACADEMY
BASICS OF PYTHON
PROGRAMMING
🔹 Python Basics Agenda Topics
1. Introduction to Python
5. Type casting
• 📌 Syntax:
print("message") # This is a comment
• 📌 Example:
print("Hello, Python!") # Output: Hello, Python!
• 📌 Syntax:
variable_name = value
• 📌 Example:
name = "Alice" # str
age = 25 # int
price = 99.99 # float
is_active = True # bool
5.Type Casting
• 🔹 Definition: Converting one data type into another.
• 📌 Syntax:
int(), float(), str(), bool()
• 📌 Example:
a = int("5") # converts string to int
b = float("4.3") # converts string to float
c = str(100) # converts int to string
• 📌 Syntax:
input("message")
print(value)
• 📌 Example:
name = input("Enter your name: ")
print("Welcome", name)
7. Conditional Statements
• 🔹 Definition: Used to make decisions in the code.
• 📌 Syntax:
if condition:
# code
elif another_condition:
# code
else:
# code
• 📌 Example:
🔹 Definition:
for loop – used when you want to iterate over a sequence (like a
list, string, range, etc.).
loop – used when you want to repeat code as long as a
while
condition is true.
1. For Loop
🔹 Definition: Repeats a block of code for each item in a sequence.
📌 Syntax:
📌 Example:
for i in range(5):
print(i)
📝 Output:
0
1
2
3
4
🔁 2. while Loop
🔹 Definition: Repeats a block of code as long as the condition is True.
📌 Syntax:
while condition:
# code block
📌 Example:
count = 0
while count < 5:
print(count)
count += 1
📝 Output:
3. break Statement
🔹 Definition: Exits the loop immediately, even if the loop condition is
still true.
📌 Syntax:
for i in range(10):
if i == 5:
break
print(i)
📝 Output:
0
1
2
3
4
🔁4 . continue Statement
🔹 Definition: Skips the current iteration and moves to the
next.
📌 Syntax:
for i in range(5):
if i == 2:
continue
print(i)
📝 Output:
0
1
3
4
🔄 Summary Table:
Keywor Purpose
d
for Loop through a sequence (list,
string, etc.)
while Loop while a condition is true
break Exit the loop
continue Skip the current iteration
9. Functions
• 🔹 Definition: Reusable block of code that performs a specific
task.
• 📌 Syntax:
def function_name(parameters): # code return value
• 📌 Example:
def greet(name): return "Hello " + name print(greet("Alice"))
• Python provides the try, except, else, and finally blocks to manage
exceptions.
Example:
try:
num = int(input("Enter a number: "))
result = 10 / num
except ValueError:
print("Invalid input! Please enter a number.")
except ZeroDivisionError:
print("Can't divide by zero!")
else:
print("Result is:", result)
finally:
print("This runs no matter what.")
Exception Description
ZeroDivisionErr Raised when dividing by zero
or
ValueError Raised when an invalid value is
used
TypeError Raised when a wrong type is
used
IndexError Raised when accessing invalid
index
KeyError Raised when a key is not in a
dict
FileNotFoundEr Raised when a file is not found
ror
🔸 Question: Create a program that stores your name, age, and is_student
status in variables. Print them all.
2. Operators
🔸 Question: Write a program to take two numbers and print the result of:
addition
subtraction
multiplication
division
modulus
🔸 Question: Ask the user to enter their name and favorite programming
language. Print a greeting like:
Hello John! You love Python!
4. Conditional Statements
🔸 Question: Write a program that takes a number from the user and checks
whether it is positive, negative, or zero.
5. Lists and Tuples
🔸 Question: Create a list of 5 fruits. Print the third fruit, and also print all
fruits using a loop.
🔸 Question: Write a for loop to print numbers from 1 to 10. If the number is
5, skip it using continue. Stop the loop if the number is 8 using break.
7. Strings
8. Dictionaries
🔸 Question: Create a dictionary with keys: "name", "age", "course". Store your
info and print each key and value.
9. Functions
🔸 Question: Create a class Student with attributes name and grade. Add a
method display() to print student details. Create an object and call the
method.
11. Exception Handling
🔸 Question: Write a program that takes two numbers from the user and
divides them. Use try-except to catch a division by zero error.