Day: February 11, 2021

Python Example – Write a Python program to swap two variables

Hits: 11 (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 = …

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

Hits: 0 (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 Example – Write a Python program to get the size of a file

Hits: 1 (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 …

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

Hits: 1 (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 …

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

Hits: 3 (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 …

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

Hits: 2 (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 …

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

Hits: 2 (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 …

Python Example – Write a Python program to concatenate N strings

Hits: 0 (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 …

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

Hits: 0 (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+”‘ = …

Python Example – Write a Python program to find the available built-in modules

Hits: 0 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the available built-in modules. Sample Solution Python Code: import sys import textwrap module_name = ‘, ‘.join(sorted(sys.builtin_module_names)) print(textwrap.fill(module_name, width=70)) Sample Output: _ast, _bisect, _codecs, _collections, _datetime, _elementtree, _functools, _heapq, _imp, _io, _locale, _md5, _operator, _pickle, _posixsubprocess, _random, _sha1, …