Tag Archives: Python for Citizen Data Scientist

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

Hits: 13 (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 …

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

Hits: 7 (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   …

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

Hits: 15 (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 …

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

Hits: 10 (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 …

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

Hits: 8 (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() …

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

Hits: 4 (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’, …

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

Hits: 7 (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 …

Python Example – Write a Python program to get numbers divisible by fifteen from a list

Hits: 4 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get numbers divisible by fifteen from a list.   Sample Solution Python Code: num_list = [45, 55, 60, 37, 100, 105, 220] result = list(filter(lambda x: (x % 15 == 0), num_list)) print(“Numbers divisible by 15 are”,result) Sample …

Python Example – Write a Python program to check if a number is positive, negative or zero

Hits: 0 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check if a number is positive, negative or zero.   Sample Solution Python Code: num = float(input(“Input a number: “)) if num > 0: print(“It is positive number”) elif num == 0: print(“It is Zero”) else: print(“It is …

Python Example – Write a Python program to retrieve file properties

Hits: 8 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to retrieve file properties.   Sample Solution Python Code: import os.path import time print(‘File :’, __file__) print(‘Access time :’, time.ctime(os.path.getatime(__file__))) print(‘Modified time:’, time.ctime(os.path.getmtime(__file__))) print(‘Change time :’, time.ctime(os.path.getctime(__file__))) print(‘Size :’, os.path.getsize(__file__)) Sample Output: File : 8dbacd90-266d-11e7-a9c1-cf681af3cdf1.py Access time : Fri …

Python Example – Write a Python program to extract the filename from a given path

Hits: 1 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to extract the filename from a given path.   Sample Solution Python Code: import os print() print(os.path.basename(‘/users/system1/student1/homework-1.py’)) print() Sample Output: homework-1.py   Write a Python program to extract the filename from a given path Free Machine Learning & Data …

Python Example – Write a Python program to get the name of the host on which the routine is running

Hits: 4 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the name of the host on which the routine is running.   Sample Solution Python Code: import socket host_name = socket.gethostname() print() print(“Host name:”, host_name) print() Sample Output: Host name: server   Write a Python program to …

Python Example – Write a Python program to get the system time

Hits: 7 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the system time. Note : The system time is important for debugging, network information, random number seeds, or something as simple as program performance.   Sample Solution: Python Code: import time print() print(time.ctime()) print() Sample Output: Thu …

Python Example – Write a Python program to check whether a string is numeric

Hits: 4 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check whether a string is numeric.   Sample Solution Python Code: str = ‘a123’ #str = ‘123’ try: i = float(str) except (ValueError, TypeError): print(‘\nNot numeric’) print() Sample Output: Not numeric   Write a Python program to check …

Python Example – Write a Python program to convert a byte string to a list of integers

Hits: 0 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert a byte string to a list of integers.   Sample Solution Python Code: x = b’Abc’ print() print(list(x)) print() Sample Output: [65, 98, 99]   Write a Python program to convert a byte string to a list …

Python Example – Write a Python program to swap two variables

Hits: 11 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to swap two variables.   Sample Solution Python Code: a = 30 b = 20 print(“\nBefore swap a = %d and b = %d” %(a, b)) a, b = b, a print(“\nAfter swaping a = %d and b = …