ADD YOUR ADDITIONAL NOTES HERE:

  • Random Values are a number generated using a large set of numbers and a mathematical algorithm which gives equal probability to all number occuring
  • Each Result from randomization is equally likely to occur Using random number generation in a program means each execution may produce a different result
  • Each Result from randomization is equally likely to occur
  • Using random number generation in a program means each execution may produce a different result

Random values can be used in coding:

import random
random_number = random.randint(1,100)
print(random_number)
def randomlist():
    list = ["apple", "banana", "cherry", "blueberry"]
    element = random.choice(list)
    print(element)
randomlist()

Real Life Examples: Dice Roll

import random
for i in range(3):
    roll = random.randint(1,6)
    print("Roll " + str(i + 1) + ":" + str(roll))

Challenge #1

Write a function that will a simulate a coinflip and print the output

import random

def coinflip():         #def function 
    randomflip = random.randint(0, 1) #picks either 0 or 1 randomly (50/50 chance of either) 
    if randomflip == 0: #assigning 0 to be heads--> if 0 is chosen then it will print, "Heads"
        print("Heads")
    else:
        if randomflip == 1: #assigning 1 to be tails--> if 1 is chosen then it will print, "Tails"
            print("Tails")

#Tossing the coin 5 times:
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()
t4 = coinflip()
t5 = coinflip()

random binary to dec converter

import random

decimal = random.randint(1,255)
binary = 0
i = 0   #set initial
num = decimal

while(num > 0):
    binary = ((num%2)*(10**i)) + binary # set binary equal to remainder of the input/2, multiplied by 10^i
    num = int(num/2) #now number equal num/2 rounded to an integer
    i += 1 #incriment by 1

#output result       
print("Your number " + str(decimal) + " in binary is " + str(binary))
Your number 204 in binary is 11001100

3.14

fill in the blanks!

Libraries

Okay, so we've learned a lot of code, and all of you now can boast that you can code at least some basic programs in python. But, what about more advanced stuff? What if there's a more advanced program you don't know how to make? Do you need to make it yourself? Well, not always.

You've already learned about functions that you can write to reuse in your code in previous lessons. But,there are many others who code in python just like you. So why would you do again what someone has already done, and is available for any python user?

Packages allow a python user to import methods from a library, and use the methods in their code. Most package come with documentation on the different methods they entail and how to use them, and they can be found with a quick google search. methods are used with the following:

Note: a method from a package can only be used after the import statement.

Some methods are always installed, such as those with the list methods which we have previously discussed. But others require a special python keyword called import. We will learn different ways to import in Challenge 1.

Sometimes we only need to import a single method from the package. We can do that with the word "from", followed by the package name, then the word "import", then the method. This will alllow you to use the method without mentioning the package's name, unlike what we did before, however other methods from that package cannot be used. To get the best of both worlds you can use "*".

To import a method as an easier name, just do what we did first, add the word "as", and write the name you would like to use that package as.

Challenge 1: Basic Libraries

  1. Find a python package on the internet and import it
  2. Choose a method from the package and import only the method
  3. import the package as a more convenient name.
import math as m
from math import *
from math import sqrt

Challenge 2: Turtle

Turtle is a python drawing package which allows you to draw all kinds of different shapes. It's ofter used to teach beginning python learners, but is really cool to use anywhere. Turtle employs a graphics package to display what you've done, but unfortunately it's kind of annoying to make work with vscode. Use: repl.it Click "+ Create", and for language, select "Python (with Turtle)"
Use: repl.it
Click "+ Create", and for language, select "Python (with Turtle)"
Documentation
Task: Have fun with turtle! Create something that uses at least 2 lines of different lengths and 2 turns with different angles, and changes at least one setting about either the pen or canvas. Also use one command that isn't mentioned on the table below(there are a lot). Paste a screenshot of the code and the drawing from repl.it

Commands
forward(pixels)
right(degrees)
left(degrees)
setpos(x,y)
speed(speed)
pensize(size)
pencolor(color)

Note: Color should be within quotes, like "brown", or "red"

from turtle import *
oogway = Turtle()

Challenge 3: Math

The math package allows for some really cool mathematical methods!

methods Action
ceil(x) returns the next higher integer (10-->10, 10.1--->11, 10.9--->11
f rounds to largest intefer less than or equal to x
factorial(x) returns factorial
gcd(x,y) returns the greatest common denominator of x and y
lcm(x,y) returns greatest least common multipe of x and y
Challenge: Create a program which asks for a user input of two numbers, and returns the following:
  • each number rounded up
  • each number rounded down
  • the lcm of the rounded down numbers
  • the gcf of the rounded up numbers
  • the factorial of each number
  • something else using the math package!
    Documentation
from math import *

x = float(input("Enter your first number"))
y = float(input("Enter your second number"))

def challenge(x,y):
    rux = ceil(x)
    ruy= ceil(y)
    rdx = round(x)
    rdy = round(y)
    signswitch = copysign(x, y)
    gcommond = gcd(rux,ruy)
    facx = factorial(x)
    facy = factorial(y)
    remainder = fmod(x,y) 
    print("rounded up: "+ str(rux) + " " + str(ruy))
    print("rounded down: "+ str(rdx) + " " + str(rdy))
    print("signs switched: "+ str(signswitch))
    print("greatest common factor of rounded #s: "+ str(gcommond))
    print("factorials: "+ str(facx) + " " + str(facy))
    print("remainder of x/y: " + str(remainder))
challenge(x,y)
rounded up: 2 4
rounded down: 2 4
signs switched: 2.0
greatest common factor of rounded #s: 2
factorials: 2 24
remainder of x/y: 2.0

lcm is only in python 9 so I did signswitch() instead (it returns a float with the absolute value of x but the sign of y)

from datetime import *
from random import *

def numberofdaysbetween():
    d1 = datetime.date(2023, 1, 1)
    d2 = datetime.date(2022, 12, 14)
    diff = d1 - d1
    print("The number of days between" + str(d1) + " and " + str(d2) + " is " + diff)
numberofdaysbetween()