Python User Input and Data Handling
Python User Input and Data Handling
ipynb - Colab
# Prompt the user to input their first name and store it in the 'fname' variable
fname = input("Input your First Name : ")
# Prompt the user to input their last name and store it in the 'lname' variable
lname = input("Input your Last Name : ")
# Display a greeting message with the last name followed by the first name
print("Hello " + lname + " " + fname)
# Prompt the user to input a sequence of comma-separated numbers and store it in the 'values' variable
values = input("Input some comma-separated numbers: ")
# Split the 'values' string into a list using commas as separators and store it in the 'list' variable
list = [Link](",")
# Convert the 'list' into a tuple and store it in the 'tuple' variable
tuple = tuple(list)
# Prompt the user to input a filename and store it in the 'filename' variable
filename = input("Input the Filename: ")
# Split the 'filename' string into a list using the period (.) as a separator and store it in the 'f_extns' variable
f_extns = [Link](".")
# Print the extension of the file, which is the last element in the 'f_extns' list
print("The extension of the file is : " + repr(f_extns[-1]))
Red Black
# Define a tuple called 'exam_st_date' containing the exam start date in the format (day, month, year)
exam_st_date = (11, 12, 2014)
# Prompt the user to input an integer and store it in the variable 'a'
a = int(input("Input an integer: "))
# Create new integers 'n1', 'n2', and 'n3' by concatenating 'a' with itself one, two, and three times, respectively
n1 = int("%s" % a) # Convert 'a' to an integer
n2 = int("%s%s" % (a, a)) # Concatenate 'a' with itself and convert to an integer
n3 = int("%s%s%s" % (a, a, a)) # Concatenate 'a' with itself twice and convert to an integer
[Link] 1/17
28/10/2025, 11:57 [Link] - Colab
# Calculate the sum of 'n1', 'n2', and 'n3' and print the result
print(n1 + n2 + n3)
Input an integer: 5
615
[Link] 2/17
28/10/2025, 11:57 [Link] - Colab
# Calculate the difference between the end date and start date
delta = l_date - f_date
# Call the "difference" function with the argument 22 and print the result
print(difference(22))
# Call the "difference" function with the argument 14 and print the result
print(difference(14))
10
3
[Link] 3/17
28/10/2025, 11:57 [Link] - Colab
return ((abs(1000 - n) <= 100) or (abs(2000 - n) <= 100))
# Call the "near_thousand" function with the argument 1000 and print the result
print(near_thousand(1000))
# Call the "near_thousand" function with the argument 900 and print the result
print(near_thousand(900))
# Call the "near_thousand" function with the argument 800 and print the result
print(near_thousand(800))
# Call the "near_thousand" function with the argument 2200 and print the result
print(near_thousand(2200))
True
True
False
False
# Define a function named "sum_thrice" that takes three integer parameters: x, y, and z
def sum_thrice(x, y, z):
# Calculate the sum of x, y, and z
sum = x + y + z
# Check if x, y, and z are all equal (all three numbers are the same)
if x == y == z:
# If they are equal, triple the sum
sum = sum * 3
# Call the "sum_thrice" function with the arguments (1, 2, 3) and print the result
print(sum_thrice(1, 2, 3))
# Call the "sum_thrice" function with the arguments (3, 3, 3) and print the result
print(sum_thrice(3, 3, 3))
6
27
# Define a function named "new_string" that takes a string parameter called "text"
def new_string(text):
# Check if the length of the "text" is greater than or equal to 2 and if the first two characters of "text" are "Is"
if len(text) >= 2 and text[:2] == "Is":
# If the conditions are met, return the original "text" unchanged
return text
else:
# If the conditions are not met, prepend "Is" to the "text" and return the modified string
return "Is" + text
# Call the "new_string" function with the argument "Array" and print the result
print(new_string("Array"))
# Call the "new_string" function with the argument "IsEmpty" and print the result
print(new_string("IsEmpty"))
IsArray
IsEmpty
# Use a while loop to append '*' to the output string 'times' number of times.
while times > 0:
output += '*'
times = times - 1 # Decrement the times variable.
[Link] 4/17
28/10/2025, 11:57 [Link] - Colab
print(output)
# Call the histogram function with a list of numbers and print the histogram.
histogram([1,2, 3, 4, 5, 6])
*
**
***
****
*****
******
# Use a while loop to append '*' to the output string 'times' number of times.
while times > 0:
output += '*'
times = times - 1 # Decrement the times variable.
# Call the reverse_histogram function with a list of numbers and print the reversed histogram.
reverse_histogram([1, 2, 3, 4, 5, 6])
******
*****
****
***
**
*
# Call the concatenate_list_data function with a list of numbers and print the result.
print(concatenate_list_data([1, 5, 12, 2]))
15122
386
462
418
344
236
[Link] 5/17
28/10/2025, 11:57 [Link] - Colab
566
978
328
162
758
918
237
# Define a function to calculate the greatest common divisor (GCD) of two numbers.
def gcd(x, y):
# Initialize gcd to 1.
gcd = 1
GCD of 12 & 17 = 1
GCD of 4 & 6 = 2
GCD of 336 & 360 = 24
[Link] 6/17
28/10/2025, 11:57 [Link] - Colab
# Test the 'sum_three' function with different sets of input values and print the results.
print(sum_three(2, 1, 2))
print(sum_three(3, 2, 2))
print(sum_three(2, 2, 2))
print(sum_three(1, 2, 3))
0
0
0
6
# Test the 'sum' function with different sets of input values and print the results.
print(sum(10, 6))
print(sum(10, 2))
print(sum(10, 12))
20
12
22
12722.79
# Calculate the distance between the two points using the distance formula.
# The formula computes the Euclidean distance in a 2D space.
distance = [Link](((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2))
6.324555320336759
def __get__(self, instance, owner): # Define the descriptor method to handle instance methods
return lambda *args, **kwargs: self(instance, *args, **kwargs) # Return a lambda that passes the instance
[Link] 7/17
28/10/2025, 11:57 [Link] - Colab
# Example usage:
Args:
A: First matrix (list of lists).
B: Second matrix (list of lists).
Returns:
Result of matrix multiplication (list of lists).
"""
if len(A[0]) != len(B):
raise ValueError("Number of columns in A must equal number of rows in B")
return result
# Example usage:
if __name__ == "__main__":
# Example matrices A and B
A = [[1, 2, 3],
[4, 5, 6]]
B = [[7, 8],
[9, 10],
[11, 12]]
# License: [Link]
[Link] 8/17
28/10/2025, 11:57 [Link] - Colab
return len(nums) == 8 and [Link](nums[4]) == 3
# Print the result of the test function applied to the 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))
# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))
# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))
# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))
Original list:
[19, 19, 15, 5, 5, 5, 1, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :
True
Original list:
[19, 15, 5, 7, 5, 5, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :
False
Original list:
[11, 12, 14, 13, 14, 13, 15, 14]
Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :
True
Original list:
[19, 15, 11, 7, 5, 6, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :
False
# License: [Link]
[Link] 9/17
28/10/2025, 11:57 [Link] - Colab
print("Original Integer:")
print(n)
# Print the result of the test function applied to the integer 'n'
print("Check whether the said integer greater than 4^4 and which is 7 mod 134 :")
print(test(n))
# Print the result of the test function applied to the modified integer 'n'
print("Check whether the said integer greater than 4^4 and which is 7 mod 134 :")
print(test(n))
# Print the result of the test function applied to the modified integer 'n'
print("Check whether the said integer greater than 4^4 and which is 7 mod 134 :")
print(test(n))
# Print the original integer again (note: the variable 'n' retains its previous value)
print("\nOriginal Integer:")
print(n)
# Print the result of the test function applied to the integer 'n' (no modification to 'n' since the previous assignment)
print("Check whether the said integer greater than 4^4 and which is 7 mod 134 :")
print(test(n))
Original Integer:
922
Check whether the said integer greater than 4^4 and which is 7 mod 134 :
True
Original Integer:
914
Check whether the said integer greater than 4^4 and which is 7 mod 134 :
False
Original Integer:
854
Check whether the said integer greater than 4^4 and which is 7 mod 134 :
True
Original Integer:
854
Check whether the said integer greater than 4^4 and which is 7 mod 134 :
True
# License: [Link]
# Define a function named 'test' that takes a list of strings 'str1' as input
def test(str1):
# Check if the second-to-last character of the last string in 'str1' is a proper substring of the last string
# and if the second-to-last character is different from the last character
return str1[len(str1) - 2] in str1[len(str1) - 1] and str1[len(str1) - 2] != str1[len(str1) - 1]
# Print the result of the test function applied to the 'str11' list
print("Check the nth-1 string is a proper substring of nth string of the said list of strings:")
print(test(str11))
[Link] 10/17
28/10/2025, 11:57 [Link] - Colab
str11 = ["a", "abb", "sfs", "oo", "ee", "sfde"]
# Print the result of the test function applied to the modified 'str11' list
print("Check the nth-1 string is a proper substring of nth string of the said list of strings:")
print(test(str11))
# Print the result of the test function applied to the modified 'str11' list
print("Check the nth-1 string is a proper substring of nth string of the said list of strings:")
print(test(str11))
# Print the result of the test function applied to the modified 'str11' list
print("Check the nth-1 string is a proper substring of nth string of the said list of strings:")
print(test(str11))
Original list:
['a', 'abb', 'sfs', 'oo', 'de', 'sfde']
Check the nth-1 string is a proper substring of nth string of the said list of strings:
True
Original list:
['a', 'abb', 'sfs', 'oo', 'ee', 'sfde']
Check the nth-1 string is a proper substring of nth string of the said list of strings:
False
Original list:
['a', 'abb', 'sad', 'ooaa', 'esdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwrew']
Check the nth-1 string is a proper substring of nth string of the said list of strings:
False
Original list:
['a', 'abb', 'sad', 'ooaa', 'esdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwsfsdfrew']
Check the nth-1 string is a proper substring of nth string of the said list of strings:
True
# Define a function named 'test' that takes a list 'li' and an integer 'i' as input
def test(li, i):
# Check if the sum of the first 'i' integers in 'li' equals 'i'
return sum(li[:i]) == i
# Print the result of the test function applied to the 'nums' list with the current value of 'i'
print(test(nums, 1))
[Link] 11/17
28/10/2025, 11:57 [Link] - Colab
print("\nOriginal list:")
print(nums)
# Print the result of the test function applied to the 'nums' list with the updated value of 'i'
print("Check the said list, where the sum of the first i integers is i: i =", i)
print(test(nums, 3))
# Print the result of the test function applied to the updated 'nums' list with the updated value of 'i'
print("Check the said list, where the sum of the first i integers is i: i =", i)
print(test(nums, 6))
# Print the result of the test function applied to the updated 'nums' list with the updated value of 'i'
print("Check the said list, where the sum of the first i integers is i: i =", i)
print(test(nums, 2))
Original list: [0, 1, 2, 3, 4, 5] Check the said list, where the sum of the first i integers is i: i = 1 False
Original list: [0, 1, 2, 3, 4, 5] Check the said list, where the sum of the first i integers is i: i = 3 True
Original list: [1, 1, 1, 1, 1, 1] Check the said list, where the sum of the first i integers is i: i = 6 True
Original list: [2, 2, 2, 2, 2] Check the said list, where the sum of the first i integers is i: i = 2 False
# License: [Link]
# Define a function named 'test' that takes a list of strings 'strs' as input
def test(strs):
# Use a list comprehension to check if each string in 'strs' is a palindrome (reads the same forwards and backwards)
return [s == s[::-1] for s in strs]
# Print the result of the test function applied to the 'strs' list
print(test(strs))
Original strings:
['palindrome', 'madamimadam', '', 'foo', 'eyes']
# License: [Link]
# Define a function named 'test' that takes a list of strings 'strs' and a prefix 'prefix' as input
def test(strs, prefix):
# Use a list comprehension to filter strings in 'strs' that start with the given 'prefix'
[Link] 12/17
28/10/2025, 11:57 [Link] - Colab
return [s for s in strs if [Link](prefix)]
# Print the result of the test function applied to the 'strs' list with the given prefix
print(test(strs, prefix))
# Print the result of the test function applied to the modified 'strs' list with the updated prefix
print(test(strs, prefix))
Original strings:
['cat', 'car', 'fear', 'center']
Starting prefix: ca
Strings in the said list starting with a given prefix:
['cat', 'car']
Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo']
Starting prefix: do
Strings in the said list starting with a given prefix:
['dog', 'donut']
# Define a function named 'test' that takes a list of strings 'words' as input
def test(words):
# Use the max function to find the string with the maximum length in 'words' based on the key=len (length of each string)
return max(words, key=len)
# Print the result of the test function applied to the 'strs' list
print(test(strs))
[Link] 13/17
28/10/2025, 11:57 [Link] - Colab
# Print the result of the test function applied to the modified 'strs' list
print(test(strs))
Original strings:
['cat', 'car', 'fear', 'center']
Longest string of the said list of strings:
center
Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Longest string of the said list of strings:
shatter
# Define a function named 'test' that takes a list of strings 'strs' and a substring 'substr' as input
def test(strs, substr):
# Use a list comprehension to filter strings in 'strs' that contain the given 'substr'
return [s for s in strs if substr in s]
# Print the result of the test function applied to the 'strs' list with the given substring
print(test(strs, substrs))
# Print the result of the test function applied to the modified 'strs' list with the updated substring
print(test(strs, substrs))
[Link] 14/17
28/10/2025, 11:57 [Link] - Colab
# Print the result of the test function applied to the modified 'strs' list with the updated substring
print(test(strs, substrs))
Original strings:
['cat', 'car', 'fear', 'center']
Substring: ca
Strings in the said list containing a given substring:
['cat', 'car']
Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Substring: o
Strings in the said list containing a given substring:
['dog', 'donut', 'todo']
Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Substring: oe
Strings in the said list containing a given substring:
[]
# Define a function named 'test' that takes a non-negative integer 'n' as input
def test(n):
# Use the map function to convert each integer in the range from 0 to 'n' (inclusive) to a string
# Then, use ' '.join to concatenate the strings with a space separator
return ' '.join(map(str, range(n + 1)))
# Print the result of the test function applied to the 'n' value
print(test(n))
# Print the result of the test function applied to the updated 'n' value
print(test(n))
Non-negative integer:
4
Non-negative integers up to n inclusive:
0 1 2 3 4
Non-negative integer:
20
Non-negative integers up to n inclusive:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# License: [Link]
[Link] 15/17
28/10/2025, 11:57 [Link] - Colab
# Return a list of lowercase letters with odd ASCII values
return [c for c in s if [Link]() and ord(c) % 2 == 0]
# Print the result of the test function applied to the 'strs' string
print(test(strs))
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
Original string:
a b c d
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
otherwise return the list of lowercase letters with odd order:
['a', 'b', 'c', 'd']
Original string:
a,b,c,d
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
otherwise return the list of lowercase letters with odd order:
['a', 'b', 'c', 'd']
Original string:
abcd
Split the said string into strings if there is a space in the string,
otherwise split on commas if there is a comma,
otherwise return the list of lowercase letters with odd order:
['b', 'd']
# License: [Link]
# Define a function named 'test' that takes a list of numbers 'nums' as input
def test(nums):
# List comprehension to create a list of maximum values for each prefix of the input list
# Iterate through the indices from 1 to the length of 'nums' + 1
# For each index 'i', find the maximum value in the prefix nums[:i]
return [max(nums[:i]) for i in range(1, len(nums) + 1)]
# Print the result of the test function applied to the 'nums' list
print(test(nums))
# Print the result of the test function applied to the updated 'nums' list
print(test(nums))
# Print the result of the test function applied to the updated 'nums' list
print(test(nums))
Original list:
[0, -1, 3, 8, 5, 9, 8, 14, 2, 4, 3, -10, 10, 17, 41, 22, -4, -4, -15, 0]
List whose ith element is the maximum of the first i elements of the said list:
[0, 0, 3, 8, 8, 9, 9, 14, 14, 14, 14, 14, 14, 17, 41, 41, 41, 41, 41, 41]
Original list:
[6, 5, 4, 3, 2, 1]
List whose ith element is the maximum of the first i elements of the said list:
[6, 6, 6, 6, 6, 6]
Original list:
[1, 19, 5, 15, 5, 25, 5]
List whose ith element is the maximum of the first i elements of the said list:
[1, 19, 19, 19, 19, 25, 25]
[Link] 17/17