Week 1: Python Basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

WHAT DOES A COMPUTER DO

A

Fundamentally:

◦performs calculations a billion calculations per second!
two operations in same time light travels 1 foot

◦remember results
100s of gigabytes of storage!
typical machine could hold 1.5M books of standard size

What kinds of calculations?
◦built-into the language
◦ones that you define as the programmer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

TYPES OF KNOWLEDGE / Declarative knowledge

A

Is statements of fact.

◦there is candy taped to the underside of one chair

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

TYPES OF KNOWLEDGE / Imperative knowledge

A

Is a recipe or “how-to”knowledge:

It uses a sequence of steps to find a solution:

1) face the students at the front of the room
2) count up 3 rows
3) start from the middle section’s left side
4) count to the right 1 chair
5) reach under chair and find it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

WHAT IS A RECIPE?

A

1) sequence of simple steps
2) flow of controlprocess that specifies when each step is executed
3) a means of determining when to stop

Steps 1+2+3 = an algorithm!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to capture a recipe in a mechanical process?

A

Fixed program computer:
◦calculator
◦Alan Turing’s Bombe

Stored program computer:
◦machine stores and executes instructions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Stored program computer?

A

Sequence of instructions stored inside computer

◦built from predefined set of primitive instructions:

1) arithmetic and logic
2) simple tests
3) moving data

Special program (interpreter) executes each instruction in order:

◦use tests to change flow of control through sequence
◦stop when done

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In computability theory what is the Turing completeness?

A

Turing showed you can compute anything using 6 primitives

Anything computable in one language is computable in any other programming language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an Expression?

A

Expressions are complex but legal combinations of primitives in a programming language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Syntax

A

Particular way that you need to write and structure your code in a language so that the computer understands.

English:
“cat dog boy” not syntactically valid
“cat hugs boy” syntactically valid

Programming language: “hi”5 not syntactically valid
3.2*5 syntactically valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Static semantics

A

Static semantics is which syntactically valid strings have meaning

◦English: “I are hungry”
syntactically valid but static semantic error

◦programming language:
3.2*5 syntactically valid
3+”hi” static semantic error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Semantics

A

Semantics is the meaning associated with a syntactically correct string of symbols with no static semantic errors.

English: can have many meanings_
◦“Flying planes can be dangerous”
◦“This reading lamp hasn’t uttered a word since I bought it?”

◦programming languages: have only one meaning but may not be what programmer intended

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

WHERE THINGS GO WRONG

A

syntactic errors:
◦common and easily caught

static semantic errors:
◦some languages check for these before running program
◦can cause unpredictable behavior

no semantic errors but different meaning than what programmer intended:
◦program crashes, stops running
◦program runs forever
◦program gives an answer but different than expected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a program?

A

A program is a sequence of definitions and commands

◦definitions evaluated
◦commands executed by Python interpreter in a shell

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a command?

A

Commands (statements) instruct interpreter to do something

Can be typed directly in a shell or stored in a file that is read into the shell and evaluated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the characteristics a an object?

A

Programs manipulate data objects

Objects have a type that defines the kinds of things programs can do to them

objects are
◦scalar (cannot be subdivided)
◦non-scalar (have internal structure that can be accessed)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the scalar objects?

A

int
represent integers, ex. 5 or -5
int, or integer: a number without a fractional part. savings, with the value 100, is an example of an integer.

float
or floating point: a number that has both an integer and fractional part, separated by a point. factor, with the value 1.10, is an example of a float.

str
String: a type to represent text. You can use single or double quotes to build a string.

bool
Represent Booleanvalues Trueand False
bool, or boolean: a type to represent logical values. Can only be True or False (the capitalization is important!).

NoneType–specialand has one value, None

You can use type() in Python to see the type of an object:

In [1]: type(5)
Out[1]: int

In [2]: type(3.0)
Out[2]: float

17
Q

What does the conversion (CAST) do?

A

Can convert object of one type to another:

float(3)converts integer 3 to float 3.0
int(3.9)truncates float 3.9to integer 3

18
Q

What forms an expression?

A

Combine objects and operators to form expressions

syntax for a simple expression

19
Q

How basic arithmetic operations work?

A
i+j  = the sum
i-j   = the difference
i*j   = the product

i/j = division
10/2=5.0
10/3=3.3333333333333335

i//j = int division
10/2=5
10/3=3

Modulo: %. This operator returns the remainder of the division of the number to the left by the number on its right.

i%j = the remainder when i is divided by j
10/2=0
10/3=1

i**j = i to the power of j

20
Q

What is the arithmetic operators precedence?

A

Operator precedence without parentheses:

**
*
/
+ and –executed left to right, as appear in expression

21
Q

What are the comparison operators on INT and FLOAT?

A

i and j are any variable names:
i > j
i > = j
i < j
i < = j
i = = j equality test, True if i equals j
i ! = j inequality test, True if i not equal to j

22
Q

What are the Logic operators in boolean?

A

a and b are any variable names:

not a True if a is False
False if a is True

a and b True if both are True

a or b True if either or both are True

23
Q

What are branching programs?

A

The simplest branching statement is a conditional

◦A test (expression that evaluates to True or False)
◦A block of code to execute if the test is True
◦An optional block of code to execute if the test is False

24
Q

What is indentation?

A

Leading white space (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.

25
Q

What is a String?

A

Letters, special characters, spaces, digits

Enclose in quotation marks or single quotes
hi = “hello there”
greetings = ‘hello’

concatenate strings
name = “eric”
greet = hi + name
greeting = hi + “ “+ name

26
Q

Operations on strings

A

concatenation
»>”a” + “bc”
‘abc’

successive concatenation
»> 3 * “bc”
‘bcbcbc’

the length
»> len(“eric”)
4

indexing

  • Begins with index 0
  • Attempting to index beyond length –1 is an error

> > > “abcd”[2]
‘c’

slicing

  • Extracts sequence starting at first index, and ending before second index
  • If no value before :, start at 0
  • If no value after :, end at length
  • If just :, make a copy of entire sequence

> > > “abcd”[0:2]
‘ab’

> > > “abcd”[:2]
‘ab’

> > > “abcd”[2:]
‘cd’

27
Q

What does the Length operation do?

A

The length of a string can be found using the len function. For example, the value of

len(‘ abc’) is 3.

28
Q

What does the Indexing operation do?

A

Indexing can be used to extract individual characters from a string. For example:

Typing ‘abc’ [0] into the interpreter will cause it to display the string
‘a’.

Typing ‘abc’ [3] will produce the error message IndexError: string index out of range. Since Python uses 0 to indicate the first element of a string, the
last element of a string of length 3 is accessed using the index 2.

Negative numbers are used to index from the end of a string. For example, the value of
‘abc’ [ -1] is ‘ c’

29
Q

What does the Slicing operation do?

A

Slicing is used to extract substrings of arbitrary length. If s is a string, the expression s [start: end] denotes the substring of s that starts at index start and ends at index end-1.

When constructing a slice, as in [6:11], the first index number is where the slice starts (inclusive), and the second index number is where the slice ends (exclusive), which is why in our example above the range has to be the index number that would occur just after the string ends.

S a m m y S h a r k !
0 1 2 3 4 5 6 7 8 9 10 11

ss = “Sammy Shark!”
print(ss[6:11])
Output: Shark

print(ss[7:])
Output
hark!

print(ss[:5])
Output
Sammy

S a m m y S h a r k !
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

print(ss[-4:-1])
Output
ark

Why does it end at index end-1 rather than end? So that expressions such as ‘abc’ [0: len(‘ abc’)] have the value one might expect. If the value before the colon is omitted, it defaults to 0.

If the value after the colon is omitted, it defaults to the length of the string. Consequently, the expression ‘abc’[:] is semantically equivalent to
the more verbose ‘abc’ [0: len (‘abc’)].

30
Q

What is Object-oriented programming?

A

Organizes the computation around a set of objects and the things that can be done to those objects.

31
Q

What does IDE mean?

A

Integrated Development Environment

32
Q

What does IDLE mean?

A

Integrated Development and Learning Environment

Interactive shell

33
Q

How do you add comments in Python?

A

You can add comments to your Python scripts. Comments are important to make sure that you and others can understand what your code is about.

To add comments to your Python script, you can use the # tag. These comments are not run as Python code, so they will not influence your result.

34
Q

type(a)

A

To find out the type of a value or a variable that refers to that value, you can use the type() function. Suppose you’ve defined a variable a, but you forgot the type of this variable. To determine the type of a, simply execute:

type(a)