Chapter 2: Introduction to Python with complete verified solutions 2025

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/75

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

76 Terms

1
New cards

Low-level versus high-level

Whether we program using instructions and data objects at the level of the machine (ie. move 64 bits of data from this location to this location) or whether we program using more abstract operations (ie. pop up a menu on the screen) that have been provided by the language designer.

2
New cards

General versus targeted to an application domain

Whether the primitive operations of the programming language are widely applicable or are fine-tuned to a domain.

3
New cards

Interpreted versus Compiled

Whether the sequence of instructions written by the programmer is executed directly, or whether it is first converted into a sequence of machine-level primitive operations.

4
New cards

Source Code

A sequence of instructions written by the programmer

5
New cards

Machine Code

Code that can be directly interpreted by the computer hardware

6
New cards

Advantages with Interpreted languages

It is often easier to debug prgrams

7
New cards

Advantages to compiled languages

They run more quickly and use less space

8
New cards

What is python good for?

Can be used to build any kind of program that does not need direct access to the computer's hardware.

Relatively simple and easy to learn.

Runtime feedback that is helpful to novice programmers.

Freely available libraries that interface to python and provide extended functionality.

9
New cards

What are the downfalls of python?

Weak static semantic checking. This makes it not optimal for programs that have high reliability constraints or are built and maintained by many people over a period of time.

10
New cards

Python Program

A sequence of definitions and commands

11
New cards

Python Script

Synonymous to Python Program

12
New cards

Shell

A window is usually associated with one of these.

This is where the definitions from the program are evaluated and the commands are executed by a python interpreter.

A new one of these is usually created whenever the execution of a program begins.

13
New cards

Command

Also known as a statement.

Instructs the interpreter to do something.

14
New cards

Objets

Core things that python programs manipulate.

Every one of these has a type

15
New cards

Type

Defines the kinds of things that programs can do with that object.

16
New cards

Scalar Objects

Objects that are indivisible.

They are like the atoms of the language.

17
New cards

Non-scalar objects

Things that have internal structure, such as strings.

18
New cards

Literals

notation for representing a fixed value in source code

19
New cards

Four Types of Scalar Objects in Python

1) int

2) float

3) bool

4) None

20
New cards

int

Represents integers.

Written in the way that we usually denote integers, which is with numbers.

21
New cards

float

Represents real numbers and always uses a decimal point.

22
New cards

Floating point numbers

There is no fixed number of digits before/after the decimal point. For instance, 1600.0, you can have as many zeroes as you want after the decimal point, or you can denote this number as 1.6e3, where the decimal point is farther to the left than the previous example.

23
New cards

bool

Used to represent boolean values, which are true or false

24
New cards

None

A type with a single value.

25
New cards

Value of the expression

When objects and operators are combined to form expressions, each of which evaluates to an object of some type. The object of some type is what this term means.

26
New cards

Example of a value

The expression 3+2 denotes the object 5 of type int

27
New cards

== operator

Whether two expressions evaluate to the same value

28
New cards

!= operator

Whether two expressions evaluate to different values

29
New cards

Shell Prompt

>>>

The interpreter is expecting the user to type some python code into the shell.

30
New cards

Python function--type

This can be used to find out the type of an object

31
New cards

Integer Division

i//j

It ignores the remainder and only returns the quotient

Ex: 6//4=1, not 1.5

32
New cards

Remainder

i%j

pronounced "i mod j" which is short for "i modulo j"

33
New cards

Exponent

i**j

If both are a type of int, this results in an int. If one is a type of float, the answer will be a float.

34
New cards

Primitive operators on type bool

and

or

not

35
New cards

bool a and b

is true if both a and b are true, false otherwise

36
New cards

bool a or b

is true if at least one of a or b is true, and false otherwise

37
New cards

bool not a

is true if a is false, and false if a is true

38
New cards

Variables

Provide a way to associate names with objects.

These are just names, they are nothing more than that.

39
New cards

Binding

The substitution of a real value for a variable/replacing the number with a machine address after the program is compiled

40
New cards

Assignment

Associates the name to the left of the = symbol with the object denoted by the expression to the right of the = symbol.

41
New cards

An object and names

Can have one, more than one, or no names associated to it.

42
New cards

How to name a variable in python

They can contain uppercase and lowercase letters, _, and digits, however they cannot start with a digit.

43
New cards

Reserved words

These are also known as keywords, and they have built-in meanings and cannot be used as variable names.

44
New cards

Examples of Reserved Words

and

as

assert

break

class

continue

def

del

elif

else

except

False

finally

for

from

global

if

import

in

is

lambda

nonlocal

None

not

or

pass

raise

return

True

try

while with

yield

45
New cards

These are considered different words in python

julie and Julie

46
New cards

Comments

Text following the pound symbol in python that is not integrated into the code, but gives the programmer insight into what the variables are doing and the functions of the program

47
New cards

Integrated Development Environment (IDE)

Provides a developer with a way to create a program, run the program, and debug the program all within one application.

48
New cards

IDLE

Part of the standard python installation package. It is an example of an IDE.

49
New cards

Popular IDE's

Anaconda and Canopy

50
New cards

What do all python IDE's provide?

1) A text editor with syntax highlighting, auto completion, and smart indentation

2) A shell with syntax highlighting

3) An integrated debugger

51
New cards

Straight Line programs

Programs that execute one statement after another in the order in which they appear, and stop when they run out of statements.

These programs are typically pretty boring.

52
New cards

Branching Programs

An instruction that tells a computer to begin executing a different part of a program rather than executing statements one-by-one

53
New cards

3 parts of a conditional statement

1) a test ie., an expression that evaluates to either True or False

2) a block of code that is executed if the test evaluates to True

3) An optional block of code that is executed if the test evaluates to false.

54
New cards

Form of a conditional statement in python

if Boolean expression:

block of code

else:

block of code

55
New cards

Advantage of indentation

Ensures that the visual structure of a program is an accurate representation of the semantic structure of that program.

56
New cards

Nested

When one block of code contains another block of code

57
New cards

elif

else if

58
New cards

Compound Boolean Expression

A combination of two or more Boolean expressions using logical operators

59
New cards

Constant Time

A program for which the maximum running time is bounded by the length of the program.

There exists a constant, k, such that the program is guaranteed to take no more than k steps to run.

The running time does not grow with the size of the input to the program.

These programs are pretty limited in what they can do.

60
New cards

Computational Complexity

The study of the intrinsic difficulty of problems

61
New cards

str

string

62
New cards

How are strings written?

With single or double quotes.

63
New cards

Overloaded

When something has different meanings, depending upon the types of the objects to which it is applied.

64
New cards

Repetition Operator

makes multiple copies of a string and joins them together

65
New cards

Type Checking

Turns careless mistakes into errors that stop execution, rather than errors that lead programs to behave in mysterious ways.

66
New cards

Length of a string

You can find the length by using the len function.

Example: value of len('abc') is 3

67
New cards

Indexing

Can be used to extract individual characters from a string. All indexing in python is zero-based.

Example: 'abc'[0] will spit out the string 'a'

Negative numbers in indexing then start at the end of the string

Example: 'abc'[-1] will spit out the string 'c'

68
New cards

Slicing

Used to extract substrings of arbitrary length.

Example: s is a string

s[start:end] denotes the substring of s that starts at index start and ends at index end -1

Example: 'abc'[1:3] will spit out 'bc'

69
New cards

Input

Takes a string as an argument and displays it as a prompt in the shell.

It waits for the user to type something, followed by hitting the enter key. The line typed by the user is treated as a string and becomes the value returned by the function.

70
New cards

Type Conversions (Type Casts)

We use the name of a type to convert values to that type.

Example: int('3')*4 is 12.

When a float is converted to an int, the number is truncated rather than rounded, so int(3.9) is the int 3

71
New cards

Unicode Standard

Character coding system designed to support the digital processing and display of the written texts of all languages.

More than 120,000 different characters.

72
New cards

How to tell python which encoding to use

# -- coding: encoding name --

73
New cards

Iteration

Something we use when we want a program to do the same thing many times

74
New cards

Looping

A generic iteration mechanism. It begins with a test, and if the test evaluates to true, the program executes the loop body once, and then goes back to reevaluate the test. It continues to do this until the test evaluates false, where then the control is to the code after the iteration statement.

75
New cards

Hand simulation

Attempting to solve code on paper instead of through a program, such as python. This is useful in understanding how a program behaves.

76
New cards

Break statement

Terminates a loop in which it is contained, and transfers control to the code immediately following the loop.

If this statement is inside a nested loop, the break will terminate the inner loop.