[Go to site: main page, start]

0% found this document useful (0 votes)
4 views6 pages

Python Lab Programs

The document provides various Python code snippets for different tasks including calculating the sum of digits, checking for prime numbers, Armstrong numbers, and palindromes using functions. It also demonstrates string manipulation methods such as splitting, converting to uppercase, finding substrings, and replacing characters. Additionally, it includes examples of displaying current date and time in various formats using the datetime module.

Uploaded by

Anu S
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)
4 views6 pages

Python Lab Programs

The document provides various Python code snippets for different tasks including calculating the sum of digits, checking for prime numbers, Armstrong numbers, and palindromes using functions. It also demonstrates string manipulation methods such as splitting, converting to uppercase, finding substrings, and replacing characters. Additionally, it includes examples of displaying current date and time in various formats using the datetime module.

Uploaded by

Anu S
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

Write a python code to find the sum of digits for the given number

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


sum = 0
while n > 0:
digit = n % 10
sum += digit
n //= 10
print("The sum of digits is:", sum)

Write a python code to check the given number is prime or not using
function

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True

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


if is_prime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")

Write a python code to check the given number is armstrong number or not using
function
def is_armstrong(n):
order = len(str(n))
sum = 0
temp = n
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if n == sum:
return True
else:
return False

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


if is_armstrong(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Write a python code to check the given number is palindrome or not using
function
def is_palindrome(n):
temp = n
rev = 0
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n //= 10
if temp == rev:
return True
else:
return False

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


if is_palindrome(num):
print(num, "is a palindrome number")
else:
print(num, "is not a palindrome number")

Assume that the variable data refers to the string "Python rules!". Use a string

method to perform the following tasks:

a. Obtain a list of the words in the string.

b. Convert the string to uppercase.

c. Locate the position of the string "rules".

d. Replace the exclamation point with a question mark

[Link] a list of the words in the string:

data = "Python rules!"

words = [Link]()

print(words)

Output:

['Python', 'rules!']
b. Convert the string to uppercase:

data = "Python rules!"

uppercase_data = [Link]()

print(uppercase_data)

Output:

PYTHON RULES!

c. Locate the position of the string "rules":

data = "Python rules!"

position = [Link]("rules")

print(position)

Output:

d. Replace the exclamation point with a question mark:

data = "Python rules!"

new_data = [Link]("!", "?")

print(new_data)

Output:

Python rules?

9. Write a Python script to display the various Date Time formats.

a) Current date and time

b) Current year

c) Month of year
d) Week number of the year

e) Weekday of the week

f) Day of year

g) Day of the month

h) Day of week

a)Current date and time:

from datetime import datetime

current_datetime = [Link]()

formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")

print("Current date and time:", formatted_datetime)

Output:

Current date and time: 2023-10-29 16:30:45

b) Current year:

from datetime import datetime

current_datetime = [Link]()

current_year = current_datetime.strftime("%Y")

print("Current year:", current_year)

Output:

Current year: 2023

c) Month of year:

from datetime import datetime

current_datetime = [Link]()
current_month = current_datetime.strftime("%B")

print("Month of year:", current_month)

Output:

Month of year: October

d) Week number of the year:

from datetime import datetime

current_datetime = [Link]()

current_week = current_datetime.strftime("%U")

print("Week number of the year:", current_week)

Output:

Week number of the year: 43

e) Weekday of the week:

from datetime import datetime

current_datetime = [Link]()

current_weekday = current_datetime.strftime("%A")

print("Weekday of the week:", current_weekday)

Output:

Weekday of the week: Saturday

f) Day of year:

from datetime import datetime


current_datetime = [Link]()

current_day_of_year = current_datetime.strftime("%j")

print("Day of year:", current_day_of_year)

Output:

Day of year: 302

g) Day of the month:

from datetime import datetime

current_datetime = [Link]()

current_day_of_month = current_datetime.strftime("%d")

print("Day of the month:", current_day_of_month)

Output:

Day of the month: 29

h) Day of week:

from datetime import datetime

current_datetime = [Link]()

current_day_of_week = current_datetime.strftime("%w")

print("Day of week:", current_day_of_week)

Output:

Day of week: 6

You might also like