Chapter-3: Numbers (Python)
For learn how to download and install python link here: How to download and install python in window
For learn how to run and execute the code in python link here:How to write and run the code
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
Example-1: Import random number between 1 and 10
Code:
from random import randint
x= randint(1,10)
print('A random number between 1 and 10:' , x)
Result:
In result it pc can choose any number randomly between 1 and 10. In my case first time it show 4 but when I again run this it show 9. After again run the Python IDLE it show 10,1,2 respectively.
- This programme choose a random number between 1 and 10 so the result vary when you run it again and again.
- In the 1st line we call random function by write, from random import randint
- In 2nd line , first we write randint and then in bracket we give the range , Here in this line x=randint(1,10) , here 1 is the starting number and 10 is the last number.
- In the last line , we call for 'x' so it randomly print the number from range 1 to 10.
Example-2:- Import Math Module in Python to solve math problem
Code:
from math import sin, pi
print('Pi is roughly', pi)
print('sin(1) = ', sin(1))
- In the 1st line, This programme take the information from math module and from math module import sin value and pi value
- In 2nd line we call for pi value and in 3rd line call for sin (1) then in result it show the value of sin and pi.
Example-3: Import math module in Python Idle to show all the math command
Code:
import math
help(math)
Result:
It show 270 line image just click on squeezed text and it show all the math module. You have to learn all these code for solve the problem.
Example-4:- Math basic like round off
Code:
print(abs(-4.3))
print(round(3.336, 2))
print(round(345.2, -1))
Result:
4.3
3.34
350.0
Explanation:
- For this you need basic knowledge of mathematics.
- In the 1st line , here we add "abs" function which make number positive. E.g., If you solve any question and in result you get answer in negative then by use of "abs" it make positive and remove negative symbol.
- In the 2nd line it round off the value upto 2 after decimal.
- In the next line it round off to -1 it means to zero. It means no digit after decimal.
Example-5:- Some math symbol
Example-6:- Python shell you can use as calculator
Explanation:- You can use this shell as calculator. You can directly put any value like 9+5 and then press enter it show you result.
No comments:
Post a Comment
If you have any doubt, let me know.