Numbers in Python

Numbers in Python

Numbers are numeric data that you can store in the memory as numbers type of variables. They are used a lot in Python programming language to do mathematical calculations or other programming operations.

There are four types of numbers (signed integers, long integers, float, and complex numbers):

  • Signed Integers: 7
  • Long Integers: 76549991L
  • Floats: 13.5
  • Complex Numbers: 6e+47J

You have to write the number without single or double quotes (“”) or (”).

Syntax

#____ProgrammerCodes.com____
variableName = Number
#____ProgrammerCodes.com____

Example 1

#____ProgrammerCodes.com____
day = 22
print(day)
#____ProgrammerCodes.com____

Result

22
numbers in python example 1

Example 2

#____ProgrammerCodes.com____
num1 = 2
num2 = 3
total = num1 * num2
print(total)
#____ProgrammerCodes.com____

Result

6
numbers in python example 2

We can add mathematical operations inside a variable!

How to find the largest number in Python ?

max() function helps you to find and output the biggest number. You will need to add these numbers as function arguments so you can use numbers directly or use tuples and lists

Syntax

#____ProgrammerCodes.com____
max(num1,num2,num3)
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
students_age = [19,20,18,19,21,20]
print("The older student age is", max(students_age) , "years old")
#____ProgrammerCodes.com____

Result

The older student age is 21 years old
how to find the largest number in python

You can put numbers as arguments inside the function max(). I did it differently by using a list that contains numbers.

How to find the smallest number in Python?

min() function is used to find the smallest number that is inside a list, tuple, or group of numbers. You just need to call the name of the function and put the groups of numbers inside the function as arguments.

Syntax

#____ProgrammerCodes.com____
min(num1,num2,num3)
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
print("What is the smallest number in this group (6798,866,330,567,718,784,654)? It is"
       , min(6798,866,330,567,718,784,654))
#____ProgrammerCodes.com____

Result

What is the smallest number in this group (6798,866,330,567,718,784,654)? It is 330
how to find the smallest number in python

How to randomly choose a number in Python?

When you add a group of numbers, tuples, or lists, it will give you a random number or something that you have added to it. Its name is choice so this function will choose something randomly and return it to you.

Syntax

#____ProgrammerCodes.com____
choice([num1,num2,num3,num4])
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
import random
print ("The winning number(876, 9, 55556, 435, 87) is: ",
         random.choice([876, 9, 55556, 435, 87]))
#____ProgrammerCodes.com____

Result

The winning number(876, 9, 55556, 435, 87) is: 55556
how to randomly choose a number in python

We have to import the module (random) in order to use the function choice(). Visit this article to know more about modules in Python.

How to generate random number in range in Python?

With randrange() Python function, you will be able to select a number from a range or group of numbers that you defined as arguments.

  • firstNumber: you choose a number that your randrange function will begin with.
  • lastNumber: you choose a number that your randrange function will stop with.
  • steps: you choose how many number will pass in order to choose the next number that will include in the range(not to select it but just to choose numbers that randrange will choose from them).

Syntax

#____ProgrammerCodes.com____
randrange(firstNumber,lastNumber,steps)
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
import random
print ("I will select a number from 10 to 100: ", random.randrange(10, 100))
#____ProgrammerCodes.com____

Result


I will select a number from 10 to 100: 24
How to generate random number in range in Python

Tutorial’s Video

Tutorial’s Question

Choose the number that is a float type:

  • 33
  • 5.3
  • 65489075L

Simple Examples

If you have learned the basics of Python and you want to see some simple examples about these concepts such as numbers, for loop, function, and so on, I suggest you visit these articles below:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *