Tag Archives: Python for Citizen Data Scientist

Python Example – Write a Python program to retrieve file properties

(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 Apr 21 …

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

(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 Science Coding …

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

(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 get the …

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

(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 Apr 29 …

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

(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 whether a …

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, …