Tag Archives: Python for Citizen Data Scientist

Python Example – Write a Python program to check whether a file path is a file or a directory

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check whether a file path is a file or a directory.   Sample Solution Python Code: import os path=”abc.txt” if os.path.isdir(path): print(“\nIt is a directory”) elif os.path.isfile(path): print(“\nIt is a normal file”) else: print(“It is a special file (socket, FIFO, …

Python Example – Write a Python program to count the number of occurrence of a specific character in a string

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to count the number of occurrence of a specific character in a string.   Sample Solution Python Code: s = “The quick brown fox jumps over the lazy dog.” print() print(s.count(“q”)) print() Sample Output: 1   Write a Python program to …

Python Example – Write a Python program to test whether all numbers of a list is greater than a certain number

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to test whether all numbers of a list is greater than a certain number.   Sample Solution Python Code: num = [2,3,4] print() print(all(x > 1 for x in num)) print(all(x > 4 for x in num)) print() Sample Output: True …

Python Example – Write a Python program to concatenate N strings

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to concatenate N strings.   Sample Solution Python Code: list_of_colors = [‘Red’, ‘White’, ‘Black’] colors = ‘-‘.join(list_of_colors) print() print(“All Colors: “+colors) print() Sample Output: All Colors: Red-White-Black     Write a Python program to concatenate N strings Free Machine Learning & …

Python Example – Write a Python program to get the size of an object in bytes

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the size of an object in bytes.   Sample Solution Python Code: import sys str1 = “one” str2 = “four” str3 = “three” print() print(“Memory size of ‘”+str1+”‘ = “+str(sys.getsizeof(str1))+ ” bytes”) print(“Memory size of ‘”+str2+”‘ = “+str(sys.getsizeof(str2))+ ” …

Python Examples for Beginners: Write a Python program to test whether the system is a big-endian platform or little-endian platform

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to test whether the system is a big-endian platform or little-endian platform. Sample Solution: Python Code: import sys print() if sys.byteorder == “little”: print(“Little-endian platform.”) else: print(“Big-endian platform.”) print() Sample Output: Little-endian platform   Write a Python program to test whether …

Python Examples for Beginners: Write a Python program to calculate midpoints of a line

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate midpoints of a line.   Sample Solution Python Code: print(‘nCalculate the midpoint of a line :’) x1 = float(input(‘The value of x (the first endpoint) ‘)) y1 = float(input(‘The value of y (the first endpoint) ‘)) x2 = float(input(‘The …

Python Examples for Beginners: Python Code to Add Two Numbers

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort files by date. Sample Solution:- Python Code: import glob import os files = glob.glob(“*.txt”) files.sort(key=os.path.getmtime) print(“n”.join(files)) Sample Output: result.txt temp.txt myfile.txt mynewtest.txt mytest.txt abc.txt test.txt   Python Examples for Beginners: Python Code to Add Two Numbers Free Machine Learning …

Python Examples for Beginners: Write a Python program to sort three integers without using conditional statements and loops

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort three integers without using conditional statements and loops.   Sample Solution Python Code: x = int(input(“Input first number: “)) y = int(input(“Input second number: “)) z = int(input(“Input third number: “)) a1 = min(x, y, z) a3 = max(x, …

Python Examples for Beginners: Write a Python program to calculate the sum of the digits in an integer

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate the sum of the digits in an integer.   Sample Solution Python Code: num = int(input(“Input a four digit numbers: “)) x = num //1000 x1 = (num – x*1000)//100 x2 = (num – x*1000 – x1*100)//10 x3 = …