Create Tables in Power BI Desktop
1. Hello World
print("Hello Students!")
print("Welcome to Python Programming")
2. Variables
name = "John"
age = 20
salary = 25000.50
print("Name:", name)
print("Age:", age)
print("Salary:", salary)
3. Taking User Input
name = input("Enter your name: ")
print("Welcome", name)
4. Addition of Two Numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
total = num1 + num2
print("Sum =", total)
5. Even or Odd Number
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
6. Find Largest Number
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print(a, "is larger")
else:
print(b, "is larger")
7. For Loop Example
for i in range(1, 11):
print(i)
Output:
1
2
3
...
10
8. Multiplication Table
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i,
"=", num * i)
9. While Loop Example
count = 1
while count <= 5:
print(count)
count += 1
10. Function Example
def greet(name):
print("Hello", name)
greet("Anita")
greet("Rahul")
11. List Example
students = ["Rahul", "Anita",
"Vivek"]
for student in students:
print(student)
12. Dictionary Example
student = {
"Name": "Rahul",
"Age": 22,
"City": "Pune"
}
print(student["Name"])
print(student["City"])
13. File Writing
file = open("sample.txt", "w")
file.write("Welcome to Python")
file.close()
print("File Created")
14. File Reading
file = open("sample.txt", "r")
content = file.read()
print(content)
file.close()
15. Mini Project – Student Result
name = input("Enter Student Name: ")
marks = int(input("Enter Marks: "))
if marks >= 35:
result = "Pass"
else:
result = "Fail"
print("Student Name:", name)
print("Marks:", marks)
print("Result:", result)
More mini projects
1. Employee Salary Calculator
name = input("Enter Employee Name: ")
salary = float(input("Enter Basic Salary: "))
hra = salary * 0.20
da = salary * 0.10
gross_salary = salary + hra + da
print("\nEmployee Name:", name)
print("Basic Salary:", salary)
print("HRA:", hra)
print("DA:", da)
print("Gross Salary:", gross_salary)
2. Voting Eligibility Checker
name = input("Enter Your Name: ")
age = int(input("Enter Your Age: "))
if age >= 18:
print(name, "is eligible to
vote.")
else:
print(name, "is not eligible to
vote.")
3. Grade Calculator
name = input("Enter Student Name: ")
marks = int(input("Enter Marks: "))
if marks >= 75:
grade = "A"
elif marks >= 60:
grade = "B"
elif marks >= 35:
grade = "C"
else:
grade = "Fail"
print("\nName:", name)
print("Marks:", marks)
print("Grade:", grade)
4. Simple Interest Calculator
principal = float(input("Enter Principal Amount:
"))
rate = float(input("Enter Rate of Interest: "))
time = float(input("Enter Time in Years: "))
si = (principal * rate * time) / 100
print("Simple Interest =", si)
5. Electricity Bill Generator
name = input("Enter Customer Name: ")
units = int(input("Enter Units Consumed: "))
bill = units * 8
print("\nCustomer:", name)
print("Units:", units)
print("Bill Amount: ₹", bill)
7. Mobile Recharge Plan
name = input("Enter Customer Name: ")
amount = int(input("Enter Recharge Amount: "))
if amount >= 299:
print("Unlimited Calls + 2GB
Data/Day")
else:
print("Basic Recharge
Plan")
9. Movie Ticket Booking
name = input("Enter Name: ")
tickets = int(input("Enter Number of Tickets: "))
price = tickets * 150
print("\nCustomer:", name)
print("Total Amount: ₹", price)
10. Loan Eligibility Checker
name = input("Enter Name: ")
salary = int(input("Enter Monthly Salary: "))
if salary >= 25000:
print("Loan Approved")
else:
print("Loan Rejected")
11. Shopping Bill Calculator
customer = input("Enter Customer Name: ")
amount = float(input("Enter Purchase Amount: "))
if amount >= 5000:
discount = amount * 0.10
else:
discount = 0
final_amount = amount - discount
print("\nCustomer:", customer)
print("Discount:", discount)
print("Final Amount:", final_amount)
12. Restaurant Bill Generator
customer = input("Enter Customer Name: ")
food_bill = float(input("Enter Food Amount: "))
gst = food_bill * 0.05
total = food_bill + gst
print("\nCustomer:", customer)
print("GST:", gst)
print("Total Bill:", total)
13. Password Validation
password = input("Create Password: ")
if len(password) >= 8:
print("Strong Password")
else:
print("Weak Password")
14. Exam Percentage Calculator
name = input("Enter Student Name: ")
m1 = int(input("Subject 1 Marks: "))
m2 = int(input("Subject 2 Marks: "))
m3 = int(input("Subject 3 Marks: "))
total = m1 + m2 + m3
percentage = total / 3
print("\nTotal:", total)
print("Percentage:", percentage)
Challenge Project for Students
Student Report Card System
Concepts covered:
- Input
- Variables
- Arithmetic
- If-Else
- Multiple
Subjects
name = input("Enter Student Name: ")
english = int(input("English Marks: "))
maths = int(input("Maths Marks: "))
science = int(input("Science Marks: "))
total = english + maths + science
percentage = total / 3
if percentage >= 75:
grade = "A"
elif percentage >= 60:
grade = "B"
elif percentage >= 35:
grade = "C"
else:
grade = "Fail"
print("\n----- REPORT CARD -----")
print("Name:", name)
print("Total:", total)
print("Percentage:", round(percentage, 2))
print("Grade:", grade)
Step 1:
Create Database in SQL Server 2019
CREATE DATABASE
TelephoneDirectory;
GO
USE TelephoneDirectory;
GO
CREATE TABLE Contacts
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(100),
MobileNo VARCHAR(20),
City VARCHAR(50)
);
Step 2:
Install Python Package
Open PyCharm
Terminal:
pip install
pyodbc
Step 3:
Python Program
import pyodbc
# SQL Server Connection
conn = pyodbc.connect(
"DRIVER={SQL Server};"
"SERVER=192.168.1.80;"
"DATABASE=TelephoneDirectory;"
"Trusted_Connection=yes;"
)
cursor = conn.cursor()
while True:
print("\n===== TELEPHONE
DIRECTORY =====")
print("1. Add Contact")
print("2. Search Contact")
print("3. View All
Contacts")
print("4. Exit")
choice = input("Enter Choice:
")
# Add Contact
if choice == "1":
name = input("Enter Name:
")
mobile = input("Enter Mobile
Number: ")
city = input("Enter City:
")
cursor.execute(
"INSERT INTO Contacts
(Name, MobileNo, City) VALUES (?, ?, ?)",
(name, mobile, city)
)
conn.commit()
print("Contact Saved
Successfully!")
# Search Contact
elif choice == "2":
search_name = input("Enter
Name To Search: ")
cursor.execute(
"SELECT * FROM Contacts
WHERE Name LIKE ?",
('%' + search_name + '%',)
)
records = cursor.fetchall()
if records:
for row in records:
print("\nID:",
row.ID)
print("Name:",
row.Name)
print("Mobile:", row.MobileNo)
print("City:",
row.City)
else:
print("No Record
Found")
# View All Contacts
elif choice == "3":
cursor.execute("SELECT *
FROM Contacts")
records = cursor.fetchall()
for row in records:
print(
row.ID,
row.Name,
row.MobileNo,
row.City
)
# Exit
elif choice == "4":
print("Thank You!")
break
else:
print("Invalid
Choice!")
conn.close()
Sample
Output
===== TELEPHONE
DIRECTORY =====
1. Add Contact
2. Search Contact
3. View All Contacts
4. Exit
Enter Choice: 1
Enter Name: Amit
Enter Mobile Number: 9876543210
Enter City: Pune
Contact Saved Successfully!
Search:
Enter Choice: 2
Enter Name To Search: Amit
ID: 1
Name: Amit
Mobile: 9876543210
City: Pune
Concepts
Students Will Learn
- input()
- while loop
- if-elif
- SQL Server connection
- INSERT
- SELECT
- fetchall()
- commit()
This is a good
beginner mini-project for Python + SQL Server.
Add this after
conn.close():
input("\nPress
Enter to Exit...")
Here's a
complete beginner-friendly Telephone Directory Project (Python + SQL Server
2019) that:
✅
Adds contacts
✅
Searches contacts
✅
Views all contacts
✅
Shows total contacts
✅
Exits properly
✅
Keeps the console open when finished
✅
Suitable for teaching students
pip install
pyodbc
import pyodbc
# SQL Server Connection
conn = pyodbc.connect(
"DRIVER={SQL Server};"
"SERVER=192.168.1.80;"
"DATABASE=TelephoneDirectory;"
"Trusted_Connection=yes;"
)
cursor = conn.cursor()
while True:
print("\n==============================")
print(" TELEPHONE DIRECTORY")
print("==============================")
print("1. Add Contact")
print("2. Search Contact")
print("3. View All
Contacts")
print("4. Total Contacts")
print("5. Exit")
print("==============================")
choice = input("Enter Your
Choice: ")
# Add Contact
if choice == "1":
print("\n--- Add New Contact
---")
name = input("Enter Name:
")
mobile = input("Enter Mobile
Number: ")
city = input("Enter City:
")
cursor.execute(
"INSERT INTO Contacts
(Name, MobileNo, City) VALUES (?, ?, ?)",
(name, mobile, city)
)
conn.commit()
print("\nContact Saved
Successfully!")
# Search Contact
elif choice == "2":
print("\n--- Search Contact
---")
search_name = input("Enter
Name To Search: ")
cursor.execute(
"SELECT * FROM Contacts
WHERE Name LIKE ?",
('%' + search_name + '%',)
)
records = cursor.fetchall()
if len(records) > 0:
print("\nSearch
Results")
print("-" * 50)
for row in records:
print("ID :", row.ID)
print("Name :", row.Name)
print("Mobile :", row.MobileNo)
print("City :", row.City)
print("-" * 50)
else:
print("\nNo Record
Found!")
# View All Contacts
elif choice == "3":
print("\n--- All Contacts
---")
cursor.execute("SELECT *
FROM Contacts")
records = cursor.fetchall()
if len(records) > 0:
print("-" * 60)
for row in records:
print(
f"ID: {row.ID} |
"
f"Name: {row.Name} |
"
f"Mobile:
{row.MobileNo} | "
f"City:
{row.City}"
)
print("-" * 60)
else:
print("\nNo Contacts
Available!")
# Total Contacts
elif choice == "4":
cursor.execute("SELECT
COUNT(*) FROM Contacts")
total = cursor.fetchone()[0]
print("\nTotal Contacts
=", total)
# Exit
elif choice == "5":
print("\nThank You For Using
Telephone Directory!")
break
else:
print("\nInvalid Choice!
Please Try Again.")
# Close Connection
conn.close()
# Hold Screen
print("\nProgram Finished Successfully.")
input("Press Enter To Close...")
SQL Server
Database Setup
Run this in SQL
Server Management Studio:
CREATE DATABASE
TelephoneDirectory;
GO
USE TelephoneDirectory;
GO
CREATE TABLE Contacts
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(100),
MobileNo VARCHAR(20),
City VARCHAR(50)
);
Create EXE
Install
PyInstaller:
pip install
pyinstaller
Create EXE:
pyinstaller
--onefile TelephoneDirectory.py
The executable
will be generated in:
dist\TelephoneDirectory.exe
This is a nice
classroom project because it covers:
- Variables
- User Input
- While Loop
- If-Else
- SQL Server Connection
- INSERT
- SELECT
- COUNT
- Functions of pyodbc
- EXE Creation using PyInstaller
------------------------Full
Programme--------------------------------
import pyodbc
# SQL Server Connection
conn = pyodbc.connect(
"DRIVER={SQL Server};"
"SERVER=192.168.1.80;"
"DATABASE=TelephoneDirectory;"
"Trusted_Connection=yes;"
)
cursor = conn.cursor()
while True:
print("\n===== TELEPHONE
DIRECTORY =====")
print("1. Add Contact")
print("2. Search Contact")
print("3. View All
Contacts")
print("4. Exit")
choice = input("Enter Choice:
")
# Add Contact
if choice == "1":
name = input("Enter Name:
")
mobile = input("Enter Mobile
Number: ")
city = input("Enter City:
")
cursor.execute(
"INSERT INTO Contacts
(Name, MobileNo, City) VALUES (?, ?, ?)",
(name, mobile, city)
)
conn.commit()
print("Contact Saved
Successfully!")
# Search Contact
elif choice == "2":
search_name = input("Enter
Name To Search: ")
cursor.execute(
"SELECT * FROM Contacts
WHERE Name LIKE ?",
('%' + search_name + '%',)
)
records = cursor.fetchall()
if records:
for row in records:
print("\nID:",
row.ID)
print("Name:",
row.Name)
print("Mobile:", row.MobileNo)
print("City:",
row.City)
else:
print("No Record
Found")
# View All Contacts
elif choice == "3":
cursor.execute("SELECT *
FROM Contacts")
records = cursor.fetchall()
for row in records:
print(
row.ID,
row.Name,
row.MobileNo,
row.City
)
# Exit
elif choice == "4":
print("Thank You!")
break
else:
print("Invalid
Choice!")
conn.close()
# Hold Screen
print("\nProgram Finished Successfully.")
input("Press Enter To Close...")
Step 1: Install Jupyter Notebook
(ensure before python is installed)
Open Command Prompt and run:
pip install notebook
To start Jupyter Notebook:
jupyter notebook
A browser window will open automatically.
The error means Pandas is not installed in the Python environment that Jupyter Notebook is using.
Step 1: Open Command Prompt
Run:
pip install pandas openpyxl
Wait for the installation to complete.
No comments:
Post a Comment