AP CSP: Python

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

+

-Add numbers (can result in integers or floats) -Concatenate Strings "Hi" + "Luke" = "HiLuke" "Hi" + 5 results in an error (string + int) "Hi" + "5" = "Hi5"

Low Level Language

-Relate to specific hardware of a particular type of computer. -Low level languages give programmers direct control of a computer. Programmers might be able to optimize code for a particular device. -Low level language is closer to what the machine understands, but farther from what a human does. Examples include: Assembly Language and Machine Code (Basically Binary)

Iterations

-Repetition of a process -Called a loop -Three parts: set up counter, check condition, update counter -Lots of different types of loops in CS: Loop, Iteration, Repeat, Do While, Do For, Do Until

Console Sessions

1) A way to run a program one line at a time (Similar to a calculator) 2) A console remembers variables during the duration of the session 3) It will display the result of each operation as an output (we do not have to print () to get an output) 4) When you end the session the variables are reset

3 parts to IDEs

1) File Manager: organize all the files in your program) 2) Source Code: where you write your program 3) Console: where you test your program/also a fancy calculator

3 Characteristics of Python

1) High Level Language 2) Interpreted 3) Object-oriented

3 Advantages of Using Functions

1) Organized 2) Easier to maintain and update 3) Code reusability

Programs are made up of three parts

1) Sequencing (goes down the code no matter what) 2) Selection (program selects/chooses where to go next) 3) Iteration (how many times a program runs)

3 Types of Errors

1) Syntax: you break the rules of the language 2) Logic: program will run with an undesired result 3) Run Time: Program will run at first but eventually crash. Typically you ask the computer to do the impossible.

Parameter

A variable used in a function to customize what it does my_function(x) x is the parameter

String

ASCII Characters "Hello" or "12:00"

Errors/Bugs

As you begin to write code you will run into three types of errors. Fixing errors is called debugging (very old computers would run incorrectly due to actual bugs!)

int()

Changes things to integers

str()

Changes things to strings You need to do this to add a string with an int.

==

Checks to see if things are equal and returns True or False 4==5 results in False

!=

Checks to see if things are not equal 4!=5 results in True

/

Divides numbers and *always stores value as a float* 4/4 = 1.0

//

Finds the quotient 5//3 results in 1

%

Finds the remainder; called mod 5%3 results in 2

=

Gets the value of, NOT "equals" x = 3 x is now assigned the value of 3

Python is a _ language.

High level -Easier for humans to understand; similar to English -Easier to write code and debug (find errors) -Portable code: not designed to run on just one type of machine

Function

In python, function is a group of related statements that perform a specific task; it's a named group of code. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes code reusable.

Program.py -> _ -> 01010101...

Interpreter -Program.py is your source file; a high level language -Interpreter: your python program must go through an interpreter before the computer knows what to do. -0101010=What your computer understands; low level language.

Local vs. Global

Lifetime of a variable is the period throughout which the variable exists in the memory. The lifetime of variables inside a function is as long as the function executes. They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.

*

Multiply numbers Multiply ints and strings 3*12=36 2*"Hi" = "HiHi" 2.0*"Hi" results in an error (this is a float and a string)

Procedure

Named block of code, AKA a function

Order of Operations

Parentheses () Exponents ** Multiplication, MOD, and Division * / // % Addition and Subtraction + - and or

Scope of Variables

Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope.

Integrated Development Environment (IDE)

Software for building applications that combines common developer tools into a single graphical user interface (GUI) -We will write our python programs using different IDE's

Argument

Specific values passed into functions my function(x) x = 3 (this is an argument) x = 5 (this is an argument)

input()

Stores user input

-

Subtract numbers (you can't subtract strings)

**

Takes a number to a power 2**3 = 8 (we do not use ^)

Return Statement

The return statement is used to exit a function and go back to the place from where it was called. It is optional, but sometimes useful. If you do not use the return statement, the function will return None. None is caused when you have the call the same procedure that's in the function (e.g. you say print() when there is already a print in the function) Example: def greet(name): print("Hello, " + name + "!") print(greet("May")) This prints: def greet(name): Hello, May! None

boolean

True or False Capital letters

Object Oriented Programming

We design blueprints for objects that we want to model by defining their attributes and behaviors. myCar.startengine() myCar.make -myCar = name of a virtual object -.startEngine() is an action you want to take with the object (behavior) -.make is information about the object (attribute)

float

a rounded decimal 3.0 or 0.666667

How function works in python

def my_function() ...insert code here... print(my_function()) When we use functions, we skip them initially until they are called. Then we follow down the list using sequencing, selection, and iterations until they end.

For Loop

for n in range (1,11): print(n) 1=starts with this value (this is included) 11=ends at this value (this is not included) Counter is 1 unless there is a third parameter in the parentheses (e.g. 1,11,2 would mean a counter of 2)

Lists

my_list = [3,4,5] Count starts with 0 at 3 print(my_list[0]) = 3 print(my_list[0:2]) = 3, 4 (you do not include the last number) print(my_list[-1]) = 5 (with negative numbers you start off on the right side; with negative numbers, the count doesn't start at 0 since there would be two 0's) Nested list: another_list = [3,4,5,[H,e,l,l,o]] print(another_list[3][0]) = H ([H,e,l,l,o] corresponds to the [3] while [0] corresponds to the H)

While Loop

n = 1 while n<=10: print(n) n+=1 n = 1 initializes counter while n<=10 is the condition check print(n) prints out the current value of n n+=1 updates the counter

Runtime Error

n = 1 while n<10: print(n) This results in the computer printing 1 forever since the counter doesn't update n=1 while n<10: print(n) n<=1 Now that the counter is updated, it will print 1 nine times.

integer

no decimal values 3 or -1000000

Hello World

print("Hello World") -Why print? -Every computer language is made up of thousands of predetermined functions -You can also write your own functions (more on this later) -Every language has a syntax -For example in Python, print("Hello, World") is okay but Print("Hello, World") is not -All languages are very picky! You have to learn to pay attention to the syntax.

Syntax Error

print("hi"+5) results in error print("hi"+str(5)) or print("hi"+"5") gets it to print correctly

Logic Error

print(num1+num2) results in the two numbers actually sticking together (e.g. 11) print(int(num1)+int(num2)) results in the two numbers adding (e.g. 2)


संबंधित स्टडी सेट्स

Accounting Chapter #5 Vocabulary

View Set

SageVantage MARK3330 Business Ethics Chapter 8 Test

View Set

Senior Business Class Flash Cards

View Set

Cardiovascular, Renal, and Hematologic Pharmacology

View Set

Quiz Five CEC Business (Key Terms)

View Set

Fit and Well Cardiovascular chapter 11

View Set