Tag Archives: Python tutorials

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 list the special variables used within the language

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to list the special variables used within the language.   Sample Solution Python Code: s_var_names = sorted((set(globals().keys()) | set(__builtins__.__dict__.keys())) – set(‘_ names i’.split())) print() print( ‘\n’.join(‘ ‘.join(s_var_names[i:i+8]) for i in range(0, len(s_var_names), 8)) ) print() Sample Output: ArithmeticError AssertionError AttributeError BaseException …

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

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 …