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 (”).
#____ProgrammerCodes.com____ variableName = Number #____ProgrammerCodes.com____
#____ProgrammerCodes.com____ day = 22 print(day) #____ProgrammerCodes.com____
#____ProgrammerCodes.com____ num1 = 2 num2 = 3 total = num1 * num2 print(total) #____ProgrammerCodes.com____
We can add mathematical operations inside a variable!
How to find the largest number in Python ?
#____ProgrammerCodes.com____ max(num1,num2,num3) #____ProgrammerCodes.com____
#____ProgrammerCodes.com____ students_age = [19,20,18,19,21,20] print("The older student age is", max(students_age) , "years old") #____ProgrammerCodes.com____
The older student age is 21 years old
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.
#____ProgrammerCodes.com____ min(num1,num2,num3) #____ProgrammerCodes.com____
#____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____
What is the smallest number in this group (6798,866,330,567,718,784,654)? It is 330
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.
#____ProgrammerCodes.com____ choice([num1,num2,num3,num4]) #____ProgrammerCodes.com____
#____ProgrammerCodes.com____ import random print ("The winning number(876, 9, 55556, 435, 87) is: ", random.choice([876, 9, 55556, 435, 87])) #____ProgrammerCodes.com____
The winning number(876, 9, 55556, 435, 87) is: 55556
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).
#____ProgrammerCodes.com____ randrange(firstNumber,lastNumber,steps) #____ProgrammerCodes.com____
#____ProgrammerCodes.com____ import random print ("I will select a number from 10 to 100: ", random.randrange(10, 100)) #____ProgrammerCodes.com____
I will select a number from 10 to 100: 24
Choose the number that is a float type: