How to make a number on python

Learn how to create a classic "Guess the Number" game in Python, with an example game and step-by-step instructions.

Making a Number in Python

Python has several ways to make a number. Depending on what you're trying to do, you might use the int() function, the float() function, or a literal number. Here's an example of each:

# Creating an int with the int() function
my_int = int(10)
print(my_int)
# Outputs: 10

# Creating a float with the float() function
my_float = float(3.14)
print(my_float)
# Outputs: 3.14

# Creating a number as a literal
my_num = 8
print(my_num)
# Outputs: 8

In Python, you can also make complex numbers, involving the imaginary unit i. For example:

# Creating a complex number
my_complex = 3 + 4j
print(my_complex)
# Outputs: (3+4j)

Finally, you can also make fractions in Python. To do this, you use the Fraction() class from the fractions module. Here's an example:

# Creating a fraction
from fractions import Fraction
my_fraction = Fraction(3, 4)
print(my_fraction)
# Outputs: 3/4

As you can see, Python has several ways to make numbers, depending on what you're trying to do. No matter which method you choose, Python makes it easy to work with numbers.

Answers (0)