Python for Business Analyst

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

(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 of integers …

Python Example – Write a Python program to swap two variables

(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 = %d” %(a, …

Python Example – Write a Python program to create a copy of its own source code

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a copy of its own source code.   Sample Solution Python Code: print() print((lambda str=’print(lambda str=%r: (str %% str))()’: (str % str))()) print() Sample Output: print(lambda str=’print(lambda str=%r: (str %% str))()’: (str % str))()   Write a Python program …

Python Example – Write a Python program to get the size of a file

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the size of a file.   Sample Solution Python Code: import os file_size = os.path.getsize(“abc.txt”) print(“\nThe size of abc.txt is :”,file_size,”Bytes”) print() Sample Output: The size of abc.txt is : 0 Bytes   Write a Python program to get …

Python Example – Write a Python program to get the ASCII value of a character

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the ASCII value of a character. ASCII, abbreviated from American Standard Code for Information Interchange, is a character encoding standard. ASCII codes represent text in computers, telecommunications equipment, and other devices. Most modern character-encoding schemes are based on ASCII, …

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))+ ” …