0% found this document useful (0 votes)
1 views9 pages

Coding

Uploaded by

hariprabha772005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views9 pages

Coding

Uploaded by

hariprabha772005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Write simple 'for' loops: print numbers 1 to 10, odd/even:


Program:
print("Numbers from 1 to 10:")
for i in range(1, 11):
print(i)
print("\nEven numbers from 1 to 10:")
for i in range(1, 11):
if i % 2 == 0:
print(i)
print("\nOdd numbers from 1 to 10:")
for i in range(1, 11):
if i % 2 != 0:
print(i)
Output:
Numbers from 1 to 10:
1
2
3
4
5
6
7
8
9
10

Even numbers from 1 to 10:


2
4
6
8
10

Odd numbers from 1 to 10:


1
3
5
7
9

2.'While' loops: Guess-the-number game or user menu:


Program:
import random
secret = [Link](1, 10)
guess = 0
while guess != secret:
guess = int(input("Guess a number between 1 and 10: "))
if guess < secret:
print("Too low! Try again.")
elif guess > secret:
print("Too high! Try again.")
else:
print(" Correct! You guessed it!")
Output:
Guess a number between 1 and 10: 5
Too low! Try again.
Guess a number between 1 and 10: 6
Correct! You guessed it!
3. Loop tracing: Predict output of given loop codes
For Loop:
for i in range (1, 4):
print ("Value of i:", i)
Output:
Value of i: 1
Value of i: 2
Value of i: 3
While Loop:
i=1
while i <= 3:
print ("i is", i)
i += 1
Output:
i is 1
i is 2
i is 3
Nested Loop:
for i in range (1, 3):
for j in range (1, 4) :
print ("i =", i, ", j =", j)
Output:
i=1,j=1
i=1,j=2
i=1,j=3
i=2,j=1
i=2,j=2
i=2,j=3
Loop with Condition:
for i in range (1, 4) :
if i %2 == 0:
print (i, "is even")
else:
print (i, "is odd")
Output:
1 is odd
2 is even
3 is odd

4. NESTED LOOPING:
for i in range(1, 6):
for j in range(i):
print("*", end="")
print()
Output:
*
**
***
****
*****

[Link] a for loop - print the squares of the first 5 numbers


print("Squares of the first 5 numbers:")
for i in range(1, 6):
print(f"{i}^2 = {i**2}")

Output:
Squares of the first 5 numbers:
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
Using a while loop - keep asking until user guesses the correct number
secret_number = 7
guess = None
print("\n Guess the secret number (between 1 and 10):")
while guess != secret_number:
guess = int(input("Enter your guess: "))
if guess != secret_number:
print("Wrong! Try again.")
print(" Correct! You guessed the number.")
Output:
Guess the secret number (between 1 and 10):
Enter your guess: 7
Correct! You guessed the number.

6. Study Python lists. Code: add, remove, update elements:


items = ["apple", "banana", "cherry"]
print("Starting list:", items)
[Link]("orange")
[Link](1, "grape")
print("After adding:", items)
items[2] = "watermelon"
print("After updating:", items)
[Link]("watermelon")
[Link](0)
print("After removing:", items)
Output:
Starting list: ['apple', 'banana', 'cherry']
After adding: ['apple', 'grape', 'banana', 'cherry', 'orange']
After updating: ['apple', 'grape', 'watermelon', 'cherry', 'orange']
After removing: ['grape', 'cherry', 'orange']

[Link] of the elements in a list:


numbers = [2, 4, 6, 8, 10]
total = 0
for num in numbers:
total += num
print("The sum is:", total)
Output:
The sum is: 2
The sum is: 6
The sum is: 12
The sum is: 20
The sum is: 30
[Link] for an element (linear search):
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
arr = [10, 25, 30, 45, 50]
target = int(input("Enter the number to search: "))
result = linear_search(arr, target)
if result != -1:
print(f"Element {target} found at index {result}")
else:
print(f"Element {target} not found in the list")
Output:
Enter the number to search: 5
Element 5 not found in the list

9. Reverse a List:
original_list = [1, 2, 3, 4, 5]
reversed_list = original_list[::-1]
print("Original List:", original_list)
print("Reversed List:", reversed_list)
Output:
Original List: [1, 2, 3, 4, 5]
Reversed List: [5, 4, 3, 2, 1]

10. Mini-practice: Combine all tasks in a single script.


import numpy as np, pandas as pd, [Link] as plt
arr = [Link]([1,2,3,4,5])
df = [Link]({"Num":arr,"Squared":arr**2,"Plus10":arr+10})
print(df)
[Link](df["Num"], df["Squared"], label="Squared")
[Link](df["Num"], df["Plus10"], label="Plus10")
[Link](); [Link]()
Output:
[Link] program using control statements:
Program:
marks = int(input("Enter your marks : "))
if marks >= 90:
grade = "A"
elif marks >= 80:
grade = "B"
elif marks >= 70:
grade = "C"
elif marks >= 60:
grade = "D"
elif marks >= 50:
grade = "E"
else:
grade = "F (Fail)"
print(f"Your Grade is: {grade}")

11. Read about if, Elif, else:


marks = 75
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: Fail")

[Link] control flow: Decision tree for user choices.


Decision Tree Example: Education Choices (Engineering or Arts & Science)
print("Welcome to the Education Decision Tree!")
print("Choose your path:")
print("1. Engineering")
print("2. Arts & Science")
choice1 = input("Enter your choice (1/2): ")
if choice1 == "1":
print("\n You chose Engineering ")
print("Which branch interests you?") print("a. Computer Science") print("b. Mechanical")
print("c. Civil") choice2 = input("Enter your choice (a/b/c): ") if choice2 == "a": print(" You
can become a Software Developer.") elif choice2 == "b": print(" You can become a
Mechanical Engineer.”) elif choice2 == "c": print(" You can become a Civil Engineer.") else:
print("Invalid choice under Engineering.") elif choice1 == "2": print("\n You chose Arts &
Science ") print("Which field do you prefer?") print("a. Arts") print("b. Science") choice2 =
input("Enter your choice (a/b): ") if choice2 == "a": print(" You can become a Writer,
Journalist, or Artist.") elif choice2 == "b": print(" You can become a Scientist, Teacher, or
Researcher.") else: print("Invalid choice under Arts & Science.") else: print("\n Invalid main
choice. Please restart the program.")

You might also like