0% found this document useful (0 votes)
12 views10 pages

Python Basics: Functions & Loops

Uploaded by

Dinesh Subedi
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)
12 views10 pages

Python Basics: Functions & Loops

Uploaded by

Dinesh Subedi
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

Greatest number among n numbers

n = int(input("How many numbers? "))

big = 0

for i in range(n):

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

if num > big:

big = num

print("Greatest number:", big)

Assign a letter grade based on marks

marks = int(input("Enter marks: "))

if marks >= 80:

grade = "A"

elif marks >= 60:

grade = "B"

elif marks >= 50:

grade = "C"

elif marks >= 40:

grade = "D"

else:

grade = "F"

print("Grade:", grade)
Check if a number is within a specified range

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

low = int(input("Enter lower limit: "))

high = int(input("Enter upper limit: "))

if low <= num <= high:

print("Number is within range")

else:

print("Number is out of range")

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

if num >= 10 and num <= 50:

print("Within range")

else:

print("Out of range")

Print numbers from 1 to 10 (for loop)

for i in range(1, 11):

print(i)

Print even numbers from 1 to 100 (while loop)

i=2

while i <= 100:

print(i)

i += 2
Sum of all odd numbers from 1 to 50

total = 0

for i in range(1, 51, 2):

total += i

print("Sum of odd numbers:", total)

Multiplication table of a given number

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

for i in range(1, 11):

print(num, "x", i, "=", num * i)

Factorial of a number

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

fact = 1

for i in range(1, num + 1):

fact *= i

print("Factorial:", fact)
Reverse of a number (while loop)

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

rev = 0

while num > 0:

rev = rev * 10 + num % 10

num //= 10

print("Reverse:", rev)

Print all prime numbers between 1 and 100

for num in range(2, 101):

for i in range(2, num):

if num % i == 0:

break

else:

print(num)

Fibonacci series up to n terms

n = int(input("Enter number of terms: "))

a, b = 0, 1

for i in range(n):

print(a, end=" ")

a, b = b, a + b
Check Armstrong number

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

temp = num

sum = 0

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

if sum == num:

print("Armstrong number")

else:

print("Not an Armstrong number")

Sum of digits of a number

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

total = 0

while num > 0:

total += num % 10

num //= 10

print("Sum of digits:", total)


First 10 multiples of a number

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

for i in range(1, 11):

print(num * i)

Sum of all elements in a list

lst = [1, 2, 3, 4, 5]

total = 0

for i in lst:

total += i

print("Sum:", total)

Print multiplication table using nested loops

for i in range(1, 6):

for j in range(1, 6):

print(i * j, end="\t")

print()

Create a list, add elements, and print it

lst = []

n = int(input("How many elements: "))

for i in range(n):

[Link](input("Enter element: "))


print("List:", lst)

Find sum of all elements in a list

lst = [10, 20, 30]

print("Sum:", sum(lst))

Find maximum and minimum in a list

lst = [5, 8, 2, 9, 1]

print("Maximum:", max(lst))

print("Minimum:", min(lst))

Remove duplicates from a list

lst = [1, 2, 2, 3, 4, 4]

new_list = list(set(lst))

print(new_list)

Sort a list in ascending order

lst = [5, 2, 9, 1]

[Link]()

print(lst)
Merge two lists

list1 = [1, 2, 3]

list2 = [4, 5, 6]

merged = list1 + list2

print(merged)

Common elements between two lists

list1 = [1, 2, 3]

list2 = [2, 3, 4]

common = set(list1) & set(list2)

print(list(common))

Square root using math module

import math

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

print("Square root:", [Link](num))


Python Program: Implicit and Explicit Type Casting
# Implicit Type Casting

a = 10 # int

b = 2.5 # float

c=a+b # int is automatically converted to float

print("Implicit Type Casting:")

print("a =", a, "type:", type(a))

print("b =", b, "type:", type(b))

print("c =", c, "type:", type(c))

print("\n----------------------\n")

# Explicit Type Casting

x = "25" # string

y = int(x) # string converted to integer

print("Explicit Type Casting:")

print("x =", x, "type:", type(x))

print("y =", y, "type:", type(y))


# Predefined correct username and password

correct_username = "admin"

correct_password = "1234"

# Take input from user

username = input("Enter username: ")

password = input("Enter password: ")

# Verify credentials

if username == correct_username and password == correct_password:

print("Login successful!")

else:

print("Invalid username or password.")

You might also like