Python Chap 6
Random number
useful for programs like dice rolling, how animal or insect might behave, statistical analysis or security for encryption.
Module
A python file that has code in it that you call in to another python program when you run it. AKA Modularization.
Random number seeds
Actually uses system time, They are pseudo-random numbers calculated by a formula. If the same seed were always used, the same random numbers would be generated. Since time changes, the chances of having the same seed are small.
Library Functions
Are built in functions in python. The basic ones like print, input etc are built in but others like random etc are called with "import random" "import math". Must write import statement at top. It tells python the name of the module that contains the function.
Remember modulus or remainder operator
Gives you remainder but will not display float numbers. I.E 1%100 = 1 2%10 = 10 5%10 = 5 BUT 100%1 = 0 10%2 = 0 10%5 = 0 11%3 = 2 (3 x 3 = 9. Remainder 2)
Value returning function
It returns a value back to the part of the program that called it. It is a group of statements to perform a specific task. It is called by another part of the program
math.hypot
Just a built in math function to calc hypotenuse.
How to create a new txt doc in a specific location
Make sure to put the r before the location and end it with the "w" file_name = open(r"C:\Users\Roger\Dropbox\Dropbox\COSC 1336\roger.txt", "w")
Square root function
Must import math first. Then use math.sqrt I.E number = math.sqrt(5)
Random and seed relation
PG212. Random import actually imports system clock and uses that as a seed value to create a random number. The systems time is down to a hundredth of a second. Because the clock keeps changing you get different numbers, some times if you want to same number repeated and NOT be random then you call random.seed
return expression
PG214 It return a value back to the part of the program that called it. Program 6-6. Also look at pg 215 figure 6-6 def sum(num1,num2): result = num1 + num2 return result
random.random or random.uniform
These will return a floating point number not an integer.
Describe value returning functions
They simplify code, enhance team work, increase the speed of development, reduce duplication.
number = random.randrange(5, 10) number = random.randrange(0, 101, 10)
This specifies a starting limit and a ending limit. But still will not show 10. The second one starts at goes in steps of 10 from 0-100 We can use random.randrange or random.randint both return and integer.
number = random.randrange(10)
This will print number from 0-9 not including 10.
number = random.uniform(1.0 , 100.)
This will randomly display numbers as floating points between 1.0-100.0. PG212
Module File
When importing a module file it must have a .py extension
number = random.random()
Will just display a number between 0.0 and 1.0 and will be a decimal number. PG212
random
it has to be imported for Python to use the random module. use it in something like this number = random. randint(1,100) This will gen a random number from 1-100 and assign it to number. Look at ch6 random numbers 2.py
math module
its a built in math function that has to be summoned in python like "import math"
randrange
its just like the range function except it return a random number. I.E number = random.randrange (5, 10) Selects a random number from 5-9 randint and randrange return integers.
To calc the area of a circle
math.pi * radius**2. Make sure to import the import math module.