Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 4 - Friday CS 113.

Similar presentations


Presentation on theme: "Week 4 - Friday CS 113."— Presentation transcript:

1 Week 4 - Friday CS 113

2 Last time What did we talk about last time? Data mining
Loops in Python

3 Project 1

4 Questions?

5 A Little More Python

6 Random numbers value = random() value = (b – a)*random() + a
In Scratch there was a green block that allowed us to create random numbers In Python, there are functions that give us random values Unfortunately, GlowScript only supports one of these methods, random(), which gives a random floating point value between 0 and 1 If you want a value between a and b, you can do the following: These are always floating point values, but you can use the int() conversation operator to make integers value = random() value = (b – a)*random() + a value = int((b – a)*random() + a)

7 Random color for a sphere
Recall that we represent color as a group of red, green, and blue intensities In Visual Python, those intensities are between 0 and 1 We can create a sphere with a random color as follows color1 = vector(1.0, 0, 0) #bright red color2 = vector(1.0, 1.0, 1.0) #white color3 = vector(0, 0, 0) #black red = random() green = random() blue = random() sphere(pos=vector(0,0,0), radius=1, color=vector(red, green, blue))

8 Review

9 Computer Basics

10 Hardware vs. software Hardware refers to physical parts of the computer Processor Memory Hard disk Monitor Software refers to the programs that run on it Operating system (Windows, Mac OS, iOS, Linux, Unix) Web browser (Internet Explorer, Safari, Firefox, Chrome) Business applications (Word, PowerPoint) Games

11 CPU The “brains” of the computer
Fetches instructions and data from memory Performs computations on the data based on the instructions Can send results to I/O A modern CPU is made of electronic circuitry embedded in a small silicon chip

12 Speed of computers How fast are computers?
I typed this PowerPoint on a computer running at 2.4 GHz That’s 2,400,000,000 cycles per second Each cycle, your computer can do something like: Add Subtract Multiply (Usually not divide)

13 Moore’s Law “The density of transistors on a CPU doubles every 18 months” Historically, this has meant that CPU speeds have doubled every 18 months We can’t make things much faster because of heat and power We can still put more “stuff” into a CPU

14 Memory Storage for all the data and instructions on your computer
Modern computers store everything as binary digits (bits) which have a value of 0 or 1. 1 byte = 8 bits 1 kilobyte (kb) = 210 bytes 1 megabyte (mb) = 220 bytes 1 gigabyte (gb) = 230 bytes 1 terabyte (tb) = 240 bytes

15 Making sense of memory Memory What Can be Stored 1 byte
One character, like Q 4 gigabytes A regular movie on DVD 1 kilobyte A poem 8 gigabytes An action movie on DVD 100 kilobytes A short story 50 gigabytes A movie on Blu-ray A low resolution photo 1 terabyte Two years of MP3 music 1 megabyte A novel 350,000 copies of War and Peace A medium resolution photo 250 DVD quality movies A minute of MP3 music 85,000 high resolution photos 1 gigabyte Two CD quality albums 24 albums of MP3 music

16 Memory Cache RAM Flash Drive Hard Drive Optical Drive
Actually on the CPU Fast and expensive RAM Primary memory for a desktop computer Pretty fast and relatively expensive Flash Drive Faster than hard drives Most commonly on USB keychains Hard Drive Secondary memory for a desktop computer Slow and cheap Optical Drive Secondary memory that can usually only be written once Very slow and very cheap

17 Input/Output Monitor Speakers Mouse Keyboard
Common visual output device Speakers Common audio output device Mouse Common input device Keyboard

18 Hard drives Pros Inexpensive Mature technology Cons Vulnerable to
Hard drives store data in magnetic patterns with a needle hovering above magnetically coated platters that spin at thousands of revolutions per minute Platter Pros Inexpensive Mature technology Cons Vulnerable to Impact Strong magnetic fields Noisy and prone to failure because of moving parts Generate heat Needle

19 Flash drives Pros Fast No moving parts No noise Very little heat
Flash drives store data in tiny charges in floating-gate transistors embedded in silicon Memory Pros Fast No moving parts No noise Very little heat Low power Cons Expensive Limited number of times it can be written to But increasing! Controller

20 Optical media Pros Very cheap Easy to mass produce Cons Slow
Plastic disc with patterns of dark, unreflective spots (pits) for 0 and reflective spots (lands) for 1 created on some reflective embedded material, often aluminum As the disc spins, a laser shines on its surface Newer optical media uses shorter wavelength light to get higher resolution Land Pros Very cheap Easy to mass produce Cons Slow Many forms are ROM Most others can only be written once Pit

21 Graphics

22 Resolution A screen has both a width and a height in pixels
The combination of these is called the resolution of the screen Higher numbers mean more pixels, allowing a more accurate representation of images Device Resolution iPhone 4 960 x 640 iPhone 6 1334 × 750  Older monitor 1024 x 768 720 HD TV 1280 x 720 1080 HD TV 1920 x 1080 4K monitor 3840 x 2160

23 RGB One system for representing color is RGB
With Red, Green, and Blue components, you can combine them to make most visible colors Combining colors is an additive process: With no colors, the background is black Adding colors never makes a darker color Pure Red added to pure Green added to pure Blue makes White RGB is a good model for computer screens CMYK is a subtractive system that is good for printers (and for painting)

24 Relationship to your eyes
The design for a CCD is related to how your eyes work The rod cells and cone cells in your eyes are responsible for your sight Rods pick up faint light and are not sensitive to different colors They are responsible for night and peripheral vision Cones are less sensitive but pick up different colors There are L, M, and S cones that pick up long, medium, and short wavelength light, respectively L-cones pick up reddish light, M-cones pick up greenish light, and S-cones pick up bluish light Photo credit:

25 Offline vs. real time rendering
Offline rendering means that the rendering has already happened when you see the images Used for television, movies, and print media Each frame produced by offline rendering often takes hours to render Real-time rendering is rendering done as you watch it, typically in an interactive way Used mostly for video games Render rates are often between 20 and 60 frames per second

26 Computer graphics pipeline
For most offline and real-time rendered graphics, the basic outline of producing images is the same Modeling is creating the 3D objects Animation is making them move Lighting and shading determine the lighting of the scene and other elements of visual appearance Rendering is the computation that determines the final image Modeling Animation Lighting and Shading Rendering

27 Sources of 3D data If you can't get an artist to model the object for you, there are a few other ways Generate the data procedurally Visualization of scientific (or other) data as spheres, cubes, or other primitives Sampling or scanning the real world Reconstruction from photographs Combinations!

28 Animation Once you have the model, you have to make it move around the scene One part of this process is rigging, which ties parts of the model together For example, pull the foot and it pulls the leg The model can be moved to different key frames Then a program can blend between them Motion capture is also a popular method for animating models The results can be more natural

29 Bits, Bytes, and Data

30 Koans of Bits It's all just bits Perfection is normal
There is want in the midst of plenty Processing is power More of the same can be a whole new thing Nothing goes away Bits move faster than thought

31 Binary Hardware A computer represents all data with 1’s and 0’s
What does that all really mean? Using semiconductor physics, we can make a tiny little piece of a microchip be in one of two states, say, OFF and ON, like a switch If we say that OFF is 0 and ON is 1, then, by using a lot of these switches, we can represent a lot of 1’s and 0’s

32 Base 2 (binary) numbers The binary number system is base 2
This means that its digits are: 0 and 1 Base 2 means that you need 2 digits to represent two, namely 1 and 0 Each place in the numeral as you move left corresponds to an increase by a factor of 2 instead of 10

33 Converting from base 2 to base 10
Each 1 in a base 2 numeral represents a power of 2 Add up all those values

34 Converting from base 10 to base 2
Find the largest power of 2 that is less than or equal to the number Subtract that power of 2 from the number Repeat the process until you have zero Every time you used a power of 2, write a 1 in the representation of the number in base 2 Otherwise, write a 0

35 ASCII Table Each character has a number associated with it
These numbers can be listed in tables The ASCII table only covers 7 bits of information (0-127) Unicode is a standard that lets us represent characters from all the world's languages

36 Storing a sound To store a sound in a computer, we slice up a wave and record the height of the wave as a number Each height value is called a sample By getting 44,100 samples per second, we get a pretty accurate picture of the wave

37 Abstraction Abstraction may be the most important idea in computer science We take your problem and look at it until we can see how it is similar to some other problem we know how to solve Details of how the computer works are hidden, making it easier for the user But it can also lead to problems An interface (also called an abstraction barrier) is how you interact with a complex system, often technology

38 What is a database? A database is a collection of data and a set of rules to organize the data by relationships A database administrator makes the rules and controls access A database management system (DBMS) is the program through which the user interacts with the database Oracle, Microsoft Access, Microsoft SQL Server, MySQL, and PostgreSQL are popular DBMSs

39 Database advantages If we want to store data, we could use regular files But databases have many advantages: Shared access for many users Minimal redundancy so that space is used efficiently Data integrity with rules that protect relationships Controlled access with authorized users Databases have also been heavily optimized for speed Users don’t need to know anything about the actual physical layout of the database on disk

40 Relational databases Almost all modern databases use the relational database model The fundamental unit of organization is a table An older format for databases was hierarchical, like a tree A table consists of rows which are records A record consists of fields or elements, which are each a specific item of data A special field which is unique for each record is called a primary key, which is used as an ID number

41 Queries A query is the name of a command given to a database by a user
Queries can: Retrieve Modify Add Delete Most databases allow commands to be issued through a variant of SQL (Structured Query Language)

42 Data mining Governments and companies collect so much data that no person could analyze it Data mining means looking for patterns in massive amounts of data Cluster analysis: Find a group of records that are probably related Like using cell phone records to find a group of drug dealers Anomaly detection: Find an unusual record Maybe someone who fits the profile of a serial killer Association rule mining: Find dependencies If people buy gin, they are also likely to buy tonic

43 Privacy Social media providers have access to lots of data
Facebook alone has details about over a billion people Can they find hidden patterns about your life? For research purposes, some sets of "anonymized" data are made public But researchers often discover that the people involved can be discovered anyway

44 Physics of Motion

45 Movement with constant velocity
Velocity gives the rate of change of location over time So, if you multiply a constant velocity by time, you'll get the change in distance In an equation, that's: 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛 𝑓𝑖𝑛𝑎𝑙 = 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛 𝑠𝑡𝑎𝑟𝑡 +𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦×𝑡𝑖𝑚𝑒

46 Simulating movement y x
At a constant velocity, you can simulate movement on a computer Take lots of little time steps Multiply the velocity by each time step to see how far you've moved The result will be accurate (up to the rounding errors inside a computer) Time step y x

47 Acceleration In real life, most things have some force acting on them, causing some acceleration Acceleration gives the rate of change of velocity over time So, if you multiply a constant acceleration by time, you'll get the change in velocity In an equation, that's: 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦 𝑓𝑖𝑛𝑎𝑙 = 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦 𝑠𝑡𝑎𝑟𝑡 +𝑎𝑐𝑐𝑒𝑙𝑒𝑟𝑎𝑡𝑖𝑜𝑛×𝑡𝑖𝑚𝑒

48 Movement with constant acceleration
We want to combine the two ideas together If you're moving with a constant acceleration, we can use a little calculus to find the total distance traveled The result is: 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛 𝑓𝑖𝑛𝑎𝑙 = 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛 𝑠𝑡𝑎𝑟𝑡 + 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦 𝑠𝑡𝑎𝑟𝑡 ×𝑡𝑖𝑚𝑒+ 1 2 𝑎𝑐𝑐𝑒𝑙𝑒𝑟𝑎𝑡𝑖𝑜𝑛× 𝑡𝑖𝑚𝑒 2

49 Trigonometry to the rescue
Velocity y velocity 𝑠𝑝𝑒𝑒𝑑×sin⁡𝜃 θ x velocity 𝑠𝑝𝑒𝑒𝑑×cos⁡𝜃 If you're moving at angle θ with respect to the horizon The x component is 𝑠𝑝𝑒𝑒𝑑×cos⁡𝜃 The y component is 𝑠𝑝𝑒𝑒𝑑×sin⁡𝜃

50 Combining components If you want to find the total speed, use the Pythagorean theorem: 𝑠𝑝𝑒𝑒𝑑 2 = 𝑠𝑝𝑒𝑒𝑑 𝑥 𝑠𝑝𝑒𝑒𝑑 𝑦 2 If you want to find the direction, you can use trigonometry again: tan 𝜃= 𝑠𝑝𝑒𝑒𝑑 𝑦 𝑠𝑝𝑒𝑒𝑑 𝑦 𝜃 =arctan 𝑠𝑝𝑒𝑒𝑑 𝑦 𝑠𝑝𝑒𝑒𝑑 𝑦

51 Python Review

52 Hello, world! The first program in many programming languages is called Hello, world It's a test to see if text input and output are working In Python, you can type the following to write the Hello, world program It will print Hello, world! on the next line print("Hello, world!")

53 Variables in Python In Scratch, we can define a variable by going to the orange Data section and clicking the Make a Variable button In Python, we assign a value to a name and that's it The = sign is the assignment operator Think of it as an arrow that points left, storing whatever is on the right into the variable We can use whatever is stored in a variable like it's a value person = "Walter White" print(person)

54 Visual Python Visual Python is an extension to Python that lets us easily manipulate 3D objects I chose Visual Python because it's easier to learn program when you can see the effects of your code Any Visual Python program should have the following line at the top of it: However, GlowScript.org includes this automatically, so it won’t be necessary for us from visual import *

55 A few simple shapes The box() command will make a box
pos, size, color are attributes The sphere() command will make a sphere pos, radius, and color are attributes redbox=box(pos=vector(4,2,3),            size=vector(8,4,6),color=color.red) ball=sphere(pos=vector(4,7,3),radius=2, color=color.green)

56 Loops in Python Good news: There are only two loops to learn in Python
The while loop runs as long as a condition is true In this case, when countdown becomes 0, the loop stops (since 0 is not less than 0) countdown = 10 while countdown > 0: print(countdown) countdown = countdown – 1 print("Blast-off!")

57 Repeating a set number of times
If you want to do something, say, 10 times in Python, you can use a for loop But you need to call the range() method to generate a list of numbers for you In this case, the loop will run 10 times, and the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 will print out for i in range(10): print(i)

58 Annoying syntax colon tab
The first line of every loop ends with a colon (:) If you don't put it there, Python will not run your code The stuff inside of a loop needs to be indented with a tab That's how you know what should be repeated and what is after the loop If you type the colon, Python automatically indents the next line for you colon for i in range(10): print(i) tab

59 Lab 4

60 Upcoming

61 Next time… Exam 1!

62 Reminders Turn in Project 1 by 11:59 tonight
Review Python Chapters 1 – 3 Review Blown to Bits Chapter 1 Study for Exam 1 next Monday


Download ppt "Week 4 - Friday CS 113."

Similar presentations


Ads by Google