Tag Archives: python for beginners

Python Example – Write a Python program to determine the largest and smallest integers, longs, floats

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to determine the largest and smallest integers, longs, floats. Sample Solution Python Code: import sys print(“Float value information: “,sys.float_info) print(“\nInteger value information: “,sys.int_info) print(“\nMaximum size of an integer: “,sys.maxsize) Sample Output: Float value information: sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250 738585072014e-308, min_exp=-1021, min_10_exp=-307, …

Python Example – Write a Python program to determine whether variable is defined or not

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to determine whether variable is defined or not. Sample Solution Python Code: try: x = 1 except NameError: print(“Variable is not defined….!”) else: print(“Variable is defined.”) try: y except NameError: print(“Variable is not defined….!”) else: print(“Variable is defined.”) Sample Output: Variable …

Python Example – Write a Python program to create a byte array from a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a bytearray from a list.   Sample Solution Python Code: print() nums = [10, 20, 56, 35, 17, 99] values = bytearray(nums) for x in values: print(x) print() Sample Output: 10 20 56 35 17 99   Write a …

Python Example – Write a Python program to display a floating number in specified numbers

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to display a floating number in specified numbers.   Sample Solution Python Code: order_amt = 212.374 print(‘\nThe total order amount comes to %f’ % order_amt) print(‘The total order amount comes to %.2f’ % order_amt) print() Sample Output: The total order amount …

Python Example – Write a Python program to compute the product of a list of integers (without using for loop)

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to compute the product of a list of integers (without using for loop).   Sample Solution Python Code: from functools import reduce nums = [10, 20, 30,] nums_product = reduce( (lambda x, y: x * y), nums) print(“Product of the numbers …

Python Example – Write a Python program to filter the positive numbers from a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to filter the positive numbers from a list.   Sample Solution Python Code: nums = [34, 1, 0, -23] print(“Original numbers in the list: “,nums) new_nums = list(filter(lambda x: x >0, nums)) print(“Positive numbers in the list: “,new_nums) Sample Output: Original …

Python Example – Write a Python program to filter the positive numbers from a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to filter the positive numbers from a list.   Sample Solution Python Code: nums = [34, 1, 0, -23] print(“Original numbers in the list: “,nums) new_nums = list(filter(lambda x: x >0, nums)) print(“Positive numbers in the list: “,new_nums) Sample Output: Original …

Python Example – Write a Python program to input a number, if it is not a number generate an error message

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to input a number, if it is not a number generate an error message.   Sample Solution Python Code: while True: try: a = int(input(“Input a number: “)) break except ValueError: print(“\nThis is not a number. Try again…”) print() Sample Output: …

Python Example – Write a Python program to remove the first item from a specified list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to remove the first item from a specified list.   Sample Solution Python Code: color = [“Red”, “Black”, “Green”, “White”, “Orange”] print(“\nOriginal Color: “,color) del color[0] print(“After removing the first color: “,color) print() Sample Output: Original Color: [‘Red’, ‘Black’, ‘Green’, ‘White’, …

Python Example – Write a Python program to make file lists from current directory using a wildcard

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to make file lists from current directory using a wildcard.   Sample Solution Python Code: import glob file_list = glob.glob(‘*.*’) print(file_list) Sample Output: [’30eb5e10-2675-11e7-a9c1-cf681af3cdf1.py’, ‘test.txt’, ‘abc.py’, ‘ mynewtest.txt’, ‘myfile.txt’, ‘logging_example.out’, ’35ba4b40-2675 -11e7-a9c1-cf681af3cdf1.py’, ’30c8bae0-2675-11e7-a9c1-cf681af3cdf1. py’, ‘abc.txt’, ‘exercise.cs’, ‘Example.cs’, ‘myfig.png’, ‘file.out ‘, ‘line.gif’, …