Robotics & Artificial Intelligence
Python modules offer a wide range of functionality. The math and random modules are some of the most commonly used modules in programming. The random module is used to generate random numbers, and the math module provides mathematical functions like sqrt() and factorial().
Write a program that generates a random integer between 1 and 100 using the random module. Then, use the math module to calculate the square root of the generated number.
Python Functions
1 Like
Answer
import random
import math
num = random.randint(1, 100)
print("Random number:", num)
result = math.sqrt(num)
print("Square root of the number:", result)Output
Random number: 66
Square root of the number: 8.12403840463596
Random number: 81
Square root of the number: 9.0
Answered By
1 Like
Related Questions
What is the purpose of the 'in' operator in Python?
- To check if a value exists in a sequence
- To start a loop
- To create a function
- To define a variable
What is the purpose of the 'pass' statement in Python?
- It terminates a loop when a condition is met
- It skips the current iteration of a loop
- It serves as a placeholder, allowing code to be syntactically correct while leaving the body empty
- It repeats the current iteration in a loop
Void functions are functions that perform actions but do not return a value. These functions often have side effects, such as printing to the console or modifying global variables.
Create a function called print_table_of_numbers(n) that prints the multiplication table of a number (n).
How can you use the randrange() function in your program?
- By importing the math module
- By importing the random module
- By importing the statistics module
- By importing the datetime module