The Python chr() function is used to get a string representing a character that corresponds to a Unicode code integer. For example, chr(97) returns the string 'a'. This function takes an integer argument and throws an error if it exceeds the range of 0 to 1,114,111. Here are some examples of using the chr() function in Python: # Get the character for ASCII code 65 print(chr(65)) # Output: A # Get the character for Unicode code point 1200 print(chr(1200)) # Output: Ұ # Get the characters for a list of integers codes = [71, 101, 101, 107, 115] chars = [chr(code) for code in codes] print(''.join(chars)) # Output: Geeks To print ‘Hello World’ using chr() , you can write: print(chr(72) + chr(101) + chr(108) + chr(108) + chr(111) + chr(32) + chr(87) + chr(111) + chr(114) + chr(108) + chr(100)) To print a list of all uppercase letters using chr() , you can write: letters = [] for i in range(65, 91): letters.append(chr(i)) print(letters) To print a smiley face emoji...
The float() function in Python is used to convert a value into a floating point number, which is a number with a decimal point. For example, float(3) returns 3.0 and float(“5.5”) returns 5.5. You can also use float() with numbers that are too large or too small for Python to represent as integers, such as 1.82e310 or -32.54e100. Here are some examples of using float() in Python: # Example 1: Convert an integer to a float x = 25 y = float(x) print(y) # Output: 25.0 # Example 2: Convert a string to a float s = "12.34" z = float(s) print(z) # Output: 12.34 # Example 3: Convert an infinity value to a float inf = "infinity" w = float(inf) print(w) # Output: inf If you have any questions about this code, you can drop a line in comment .
The Python bool() function returns a Boolean value (True or False) for a given object. The object can be any data type, such as numbers, strings, lists, tuples, sets, dictionaries, etc. The bool() function follows some rules to evaluate the truth value of an object: Any numeric value that is not zero is True. Zero (0) is False. Any non-empty string is True. Empty string (‘’) is False. Any non-empty collection (list, tuple, set, dictionary) is True. Empty collection ([], (), {}, set()) is False. None is always False. True and False are keywords that represent the Boolean values. Here are some examples of using bool() function: # Example 1: Convert different data types to Boolean with bool() function num = 10 print(bool(num)) # Output: True str = "Hello" print(bool(str)) # Output: True lst = [] print(bool(lst)) # Output: False none = None print(bool(none)) # Output: False # Example 2: Use bool() function in conditional statements name = input("Enter your name: ...
There are different ways to print readable results in Python depending on what kind of data you have. For example: If you have a complex data structure such as a nested dictionary or list, you can use pprint module to format it nicely with indentation and line breaks. For example: import pprint data = {"name": "Alice", "age": 25, "hobbies": ["reading", "coding", "gaming"]} pprint.pprint(data) # Output: {'age': 25, 'hobbies': ['reading', 'coding', 'gaming'], 'name': 'Alice'} If you have a numerical data such as a matrix or an array, you can use numpy module to print it with precision and alignment. For example: import numpy as np matrix = np.array([[1.2345, 2.3456], [3.4567, 4.5678]]) np.set_printoptions(precision=2) print(matrix) # Output: [[1.23 2.35] [3.46 4.57]] If you have a text data such as a string or a paragraph, you can use f-strings to format it w...
The exec() function in Python is used to execute a dynamically generated code, which is a code that is created or modified at runtime. The exec() function takes a string or an object that contains a valid Python code and executes it as if it was written in a file. You can also pass a dictionary of global and local variables to the exec() function to modify the execution environment. Here are some examples of using exec() in Python: Execute a Python statement stored as a string: code = 'print("Hello, world!")' exec(code) Output: Hello, world! In this example, exec(code) executes the Python statement 'print("Hello, world!")', which prints the string "Hello, world!" to the console. Use exec() to execute a Python script stored in a file: with open('script.py', 'r') as file: code = file.read() exec(code) In this example, we open the file 'script.py', read its contents into a string, and then use exec() to execu...
Comments
Post a Comment