Tag Archives: Python tutorials

Python Exercise: Write a python program to access environment variables

(Python Example for Citizen Data Scientist & Business Analyst)   Write a python program to access environment variables. Sample Solution:- Python Code: import os # Access all environment variables print(‘*———————————-*’) print(os.environ) print(‘*———————————-*’) # Access a particular environment variable print(os.environ[‘HOME’]) print(‘*———————————-*’) print(os.environ[‘PATH’]) print(‘*———————————-*’) Sample Output: *———————————-* environ({‘LESSOPEN’: ‘| /usr/bin/lesspipe %s’, ‘_’: ‘/usr/bin/timeout’, ‘LIBVIRT_DEFAULT_URI’: ‘qemu:///system ‘, ‘HOME’: …

Python Exercise: Write a Python program to print to stderr

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print to stderr. Sample Solution Python Code: from __future__ import print_function import sys def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) eprint(“abc”, “efg”, “xyz”, sep=”–“) Sample Output: abc–efg–xyz   Python Exercise: Write a Python program to print to stderr Free Machine Learning …

Python Exercise: Write a Python program to determine profiling of Python programs

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to determine profiling of Python programs. Note: A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module. Sample Solution …

Python Exercise: Write a Python program to print without newline or space

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print without newline or space?   Sample Solution Python Code: for i in range(0, 10): print(‘*’, end=””) print(“n”) Sample Output: **********   Python Exercise: Write a Python program to print without newline or space Free Machine Learning & Data …

Python Example – Write a Python program to list all files in a directory in Python

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to list all files in a directory in Python. Sample Solution Python Code: from os import listdir from os.path import isfile, join files_list = [f for f in listdir(‘/home/students’) if isfile(join(‘/home/students’, f))] print(files_list); Sample Output: [‘3ff44d80-2423-11e7-807b-bd9de91b1602.py’, ‘f5272c90-2423-11e7-807b-bd9de91b1602.py’, ‘test.txt’, ‘f34e26d0-2 423-11e7-807b-bd9de91b1602.py’, …

Python Example – Write a Python program to parse a string to Float or Integer

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to parse a string to Float or Integer. Sample Solution Python Code: n = “246.2458” print(float(n)) print(int(float(n))) Sample Output: 246.2458 246   Python Example – Write a Python program to parse a string to Float or Integer Free Machine Learning …

Python Example – Write a Python program to find out the number of CPUs using

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find out the number of CPUs using. Sample Solution Python Code: import multiprocessing print(multiprocessing.cpu_count()) Sample Output: 4   Python Example – Write a Python program to find out the number of CPUs using Free Machine Learning & Data Science …

Python Example – Get the path and name of the file that is currently executing

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a python program to get the path and name of the file that is currently executing. Sample Solution Python Code: import os print(“Current File Name : “,os.path.realpath(__file__)) Sample Output: Current File Name : /home/students/fb6e28e0-2425-11e7-807b-bd9de91b1602.py   Get the path and name of the file …

Python Example – Get OS name, platform and release information

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get OS name, platform and release information. Sample Solution Python Code: import platform import os print(os.name) print(platform.system()) print(platform.release()) Sample Output: posix Linux 4.4.0-47-generic   Python Example – Get OS name, platform and release information Free Machine Learning & Data …

Python Example – Check whether Python shell is executing in 32bit or 64bit mode on OS

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to determine whether a Python shell is executing in 32bit or 64bit mode on OS?   Sample Solution Python Code: # For 32 bit it will return 32 and for 64 bit it will return 64 import struct print(struct.calcsize(“P”) * …