AP CSP Unit 3: Decisions and Algorithms

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

multiplication

*

power

**

constants

*fixed values* such as numbers, letters, and strings --- they're called "constants" because their value doesn't change - numeric constants are as you expect (print 123 --> 123) - string constants use single quotes (') or double quotes (") (print 'Hello world' --> Hello world)

what is output by the following program statement? print(12 + 3 * 4)

24

<pi> and <e> are part of <math> import math print(math.pi) print(math.e)

3.141592653589793 2.718281828459045

the operator + is used with all data types n1 = 100 n2 = 200 n3 = n1 + n2 print(n3) f1 = 1.1 f2 = 22.22 f3 = f1 + f2 print(f3) s1 = "Hello" s2 = "World" s3 = s1 + s2 print(s3) b1 = True b2 = False b3 = b1 + b2 print(b3)

300 23.32 HelloWorld 1 ^^(for the last part, the value of True is considered as 1, whereas False is 0. so in Boolean algebra, True + False = 1 + 0 = 1)

swapping variables with simultaneous assignment n1 = 333 n2 = 777 print(n1,n2) n1,n2 = n2,n1 print(n1,n2)

333 777 777 333

functions can be used as parameters import math print(math.sqrt(math.sqrt(256))

4.0

python math operators: remainder division x = 14 x = x % 8 print(x) OR x %= 8

6

using the random import shortcut from random import * seed(12345) print(randint(10,99)) print(random()) print() seed(12345) print(randint(10,99)) print(random())

63 0.7326852754516092 63 0.7326852754516092

what is output by the following program statement? print(2 ** (3 // 2 * 3))

8

what is true about this program statement? print(12 + 3 // 4 ** 2) A) it will cause an error message B) it uses mathematical rules of precedence C) it uses Python rules of precedence D) it evaluates from left to right

B) it uses mathematical rules of precedence

when was python developed? by who?

in the early 1990's by guido van rossum

IDE stands for...

integrated development environment

what is the output of the following program segment print()

it skips a line with a CRLF (CRLF = carriage return and line feed)

how does python's syntax compare to other popular languages such as Java and C++?

its syntax is much simpler and cleaner

<eval> can evaluate expressions result = eval(input("Enter expression ")) print(result) OR expression = input("Enter expression ") result = eval(expression) print(expression) print(result)

Enter expression [12 + 13 25 Enter expression [12 ** 2 12 ** 2 144

keyboard input with a prompt x = input("Enter info 1 ") y = input("Enter info 2 ") print(x + y)

Enter info 1 [333 Enter info 2 [555 333555

numeric input requires <eval> x = eval(input("Enter info 1 ")) y = eval(input("Enter info 2 ")) print(x + y) OR s1 = input("Enter info 1 ") s2 = input(Enter info 2 ") print(s1 + s2) n1 = eval(s1) n2 = eval(s2) print(n1 + n2)

Enter info 1 [333 Enter info 2 [555 888 Enter info 1 [333 Enter info 2 [555 333555 888

what is python named after?

Monty Python's Flying Circus

print(4 + 5 * 2) print(4 * 5 + 2) print((4 + 5) * 2) print(4 ** 2 * 3) print(4 ** (2 * 3))

Python follows rules of precedence 14 22 18 48 4096

remainder

%

boolean logic variables b1 = True b2 = False print(b1) print(b2)

("Boolean" = a result that can only have one of two possible values: true or false) (also notice that there's no "" around True or False when assigning to the variables) True False

results when import math is not in the program... print(math.sqrt(100)) print(math.sqrt(1.44))

Traceback (most recent call last): File "filename.py", line #, in <module> print(math.sqrt(100)) NameError: name 'math' is not defined

what type of programming language is python?

a high-level dynamic object-oriented programming language

variable

a named place in the memory where a programmer can store data and later retrieve the data using the variable "name" programmers get to choose the names of the variables you can change the contents of a variable in a later statement x = 12.2 y = 14 x = 100 (so x would turn out to be 100 and y to be 14)

CPython

a python implementation Classic Python is the fastest, most up to date and complete implementation of Python

IronPython

a python implementation Iron Python is a Python implementation for the Microsoft designated Common Language Runtime. You can use all of the CLR libraries and frameworks

Jython

a python implementation Jython is a Python implementation that is Java Virtual Machine compliant. you can use all of the Java class libraries

an IDE includes

a text editor, a file manager, and an output window that displays errors of program output

what is output by the following program statement? print(2 ** (3 // 2 * 3)

an error message

run-time error

any error that occurs when the programcompiles and runs, but produces unexpected results. often called a *logic error*

why did the creator of python want to develop it?

because he was dissastisfied with the other languages and wanted to create smaller programs that didn't have to run at optimum speed

numeric expressions

because of the lack of mathematical symbols on computer keyboards, we use "computer speak" to express the classic math operations asterick is multiplication exponentiation (raise to a power) looks different from in math

what are low-level languages?

computer hardware can only understand a very low level language known as machine language

the print command can be used to...

display literal strings, variable values, and evluate expressions

what is the ouput of the following program segment? print("Tom Jones") Print("Kensington, Maryland")

error message

compile-time error

error that occurs when you make a typing mistake (something is wrong according to the rules of the language)

exception

error that occurs while the program is syntactically correct, but impossible to perform

what was important to the developer of the python language?

that he could author quickly and update accordingly

what are high-level computer languages?

they're are designed to be used and understood by humans

using import...

use an import to access functions that are part of a library that is not automatically loaded by Python during program execution place the import statement before any program code that uses a member of the import library like: import math n = math.sqrt(100) print(n)

print("AAA") print() print("BBB") print("\n") print("CCC")

using "\n" skips a line AAA BBB CCC

assignment statements

we assign a value to a variable using the assignment statement (=) an assignment statement consists of an expression on the right hand side and a variable to store the result x = 3.9 * x * (1-x)

operator precedence

when we string operators together, python must know which one to do first ("operator precedence") (order of evaluation)

output with literal strings and variables x = 100 y = 200 z = 300 print("x =", x) print("y =", y) print("z =", z)

x = 100 y = 200 z = 300

is this how you correctly swap variables? x = 100 y = 200 temp = x x = y y = temp print(x,"",y)

yes

reserved words

you cannot use reserved words as variable names / identifiers and, del, for, is, raise, assert, elif, from, lambda, return, break, else, global, not, try, class, except, if, or, while, continue, exec, import, pass, yield, def, finally, in, print

addition

+

subtraction

-

how does program execution work?

- interpreters simulate a computer that understands a high-level language - the source program is not translated into machine language all at once - an interpreter analyzes and executes the source code instruction by instruction

what are some key characteristics of python?

- it's a cross-platform language (it runs on all major hardware platforms and operating systems) - it has a rich set of supporting libraries - it's free - it's case sensitive (upper and lowercase matters!)

python variable name rules

- must start with a letter or underscore - must consist of letters, numbers, and underscores - case sensitive good: spam, eggs, spam23, _speed bad: 23spam, #sign, var.12 different: spam, Spam, SPAM

what's the difference between compiling and interpreting (in program execution)?

- once a program is compiled, it can be executed over and over without the source code or compiler. if it is interpreted, the source code and interpreter are needed each the program runs - compiled programs generally run faster since the translation of the source code happens only once

writing code with operator precedencde

- remember the rules top to bottom (parantheses, power, multiplication, addition, left to right) - when writing code, use parentheses - when writing code, keep mathematical expressions simple enough that they are easy to understand - break long series of mathematical operations up to make them more clear

x = 2 x = x + 2 print x

1. assignment statement 2. assignment with expression 3. print statement

division

/

floor

//

python math operators: real number division x = 6 x = x / 5 print(x) OR x /= 5

1.2

python math operators: exponentiation x = 1.2 x = x ** 2 print(x) OR x **=2

1.44

python math operators: subtraction x = 15 x = x - 5 print(x) OR x -= 5

10

<import math> gives access to a library with a set of math functions import math print(math.sqrt(100)) print(math.sqrt(1.44))

10.0 1.2

there are two ways to use a library: import math gives function access only with library name import math print(math.sqrt(100)) from math import * gives access with function name only from math import * print(sqrt(100))

10.0 10.0

using the math import shortcut from math import * print(sqrt(100)) print(floor(4.99999)) print(ceil(4.000001)) print(pi) print(e) print(round(4.5)) print(abs(-50))

10.0 4 5 3.141592653589793 2.718281828459045 4 50

python math operators: multiplication x = 10 x = x * 10 print(x) OR x *= 10

100

integer variables n1 = 100 n2 = 200 n3 = 300 print(n1) print(n2) print(n3)

100 200 300

Python allows simultaneous assignment n1,n2,n3 = 11,22,33 f1,f2,f3 = 1.1,2.2,3.3 s1,s2,s3 = "AAA", "BBB", "CCC" b1,b2,b3 = True,False,True print(n1,n2,n3) print(f1,f2,f3) print(s1,s2,s3) print(b1,b2,b3)

11 22 33 1.1 2.2 3.3 AAA BBB CCC True False True

what is output by the following program statement? print(2 ** 3 / 2 * 3)

12.0

<abs> and <round> are built in Python <import math> is not required print(round(12345.499999)) print(round(12345.5)) print(abs(-100))

12345 12346 100

function <math.ceil> rounds to the next whole number function <math.floor> rounds down to the previous whole number import math print(math.ceil(12345.12345)) print(math.floor(12345.12345))

12346 12345

python math operators: integer (floor) division x = 100 x = x // 7 print(x) OR x //= 7

14

python math operators: addition x = 10 x = x + 5 print(x) OR x += 5

15

what is output by the following program statement? print(12 + 13 // 4)

15

what is output by the following program statement? print(12 + 13 / 4)

15.25

<type> identifies the data type n = 100 print(type(n)) print(type(3.14159)) print(type("qwerty")) print(type(True))

<class 'int'> <class 'float'> <class 'str'> <class 'bool'>

x = input() print(x)

<input> waits for keyboard entry [Hello computer. Are you happy? Hello computer. Are you happy?

print(100)

<print> displays a numerical constant 100

print("Hello World", 100)

<print> displays a string and a number Hello World 100

print("Hello World")

<print> displays a string constant Hello World

print(10 + 3) print() print("10 + 3 =", 10 + 3)

<print> evaluates an expression and then displays the result 13 10 + 3 = 13

print(10 % 3)

<print> evaluates an operation (% = modulus division, takes remainder) 1

print(10 ** 3)

<print> evaluates an operation (** = exponent / to the power of) 1000

print(10 // 3)

<print> evaluates an operation (// = floor division, *rounds down* to the nearest whole number) 3

print(10 + 3)

<print> evaluates an operation 13

print(10 / 3)

<print> evaluates an operation 3.3333333333333335

print(10 * 3)

<print> evaluates an operation 30

print(10 - 3)

<print> evaluates an operation 7

print("Suzie Snodgrass") print("100 Orleans Court") print("Kensington, MD 20795")

<print> includes a CRLF Suzie Snodgrass 100 Orleans Court Kensington, MD 20795

string variables s1 = "A" s2 = "Howdy" s3 = "The quick brown fox jumps over the lazy dog" print(s1) print(s2) print(s3)

A Howdy The quick brown fox jumps over the lazy dog

real number or float type variables f1 = 1.1 f2 = 22.22 f3 = 333.333 print(f1) print(f2) print(f3)

floats are decimal values like 1335, 2897.11, and 3571.213 1.1 22.22 333.333

operator precedence rules

highest precedence to lowest precedence rule: - parentheses are always respected - exponentiation (raise to a power) - multiplication, division, and remainder - addition and subtraction - left to right (^^note that multiplication, division, and reminder are on the same level)

is this how you correctly swap variables? x = 100 y = 200 x = y y = x print(x,"",y)

no

how fast is python's growth?

python is the fastest-growing major programming language in the world

<random()> generates random numbers in the [0.0..1.0) range import random print(random.random()) print(random.random()) print(random.random()) print(random.random())

run 1: 0.20693759754773122 0.7080710600770302 0.8313700033765519 0.6277671214979221 run 2: 0.9936531000932808 0.4088984378771876 0.7457597061762423 0.06483275376887221

using <seed()> generates the same random numbers for each execution import random random.seed(12345) print(random.random()) print(random.random()) print(random.random()) print(random.random())

run 1: 0.41661987254534116 0.010169169457068361 0.8252065092537432 0.2986398551995928 run 2: 0.41661987254534116 0.010169169457068361 0.8252065092537432 0.2986398551995928

<randint(a,b) generates random integers in the [a...b] range import random print(random.randint(1,10)) print(random.randint(1,10)) print(random.randint(1,10)) print(random.randint(1,10)) print(random.randint(1,10))

run 1: 6 9 5 10 2 run 2: 1 8 1 9 5

<seed()> can also be used with generating the same random integers import random random.seed(123.321) print(random.randint(10,99)) print(random.randint(10,99)) print(random.randint(10,99)) print(random.randint(10,99))

run 1: 67 75 43 90 run 2: 67 75 43 90

<seed()> can also be used with generating the same random integers import random random.seed(3.14159) print(random.randint(10,99)) print(random.randint(10,99)) print(random.randint(10,99)) print(random.randint(10,99))

run 1: 87 69 95 71 run 2: 87 69 95 71

self-documenting variable

self-documenting names are like: "yearlySales" "interestRate" "firstName" rather than "boohiss", "x", or "whoknows" ^^it's better to use self-documenting names

flowchart of programming language?

source code (program) --> compiler --> machine code | v inputs --> running program --> outputs


Set pelajaran terkait

Business Law chapter 18 Application Test

View Set

Psychology Exam 4 - Chapters 11,12,14 (Multiple Choice Quizzes)

View Set

Azija-Podnebje,rastlinstvo,reke.

View Set

Final Exam Biomechnics KIN 3309 U of H

View Set

Caring for a Jackson-Pratt or Hemovac Drain, and Penrose Drain

View Set

Bolton SOC 411 Exam 2 Textbook terms

View Set

MKT 310: Consumer Behavior - Exam 3 (Chapters 8-13)

View Set

Pressure Ulcers, Wounds, and Wound Management

View Set

Statistics - Week 3 HW Questions

View Set

CFP Investment Planning Securities Markets and Money Market Instruments

View Set