CSC 203 chapter 2: Introduction to Python Programming

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/63

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.

64 Terms

1
New cards

statement

Specifies a task to perform

  • In: 45+72

  • Ou: 117

2
New cards

assignment statement

creates a variable and uses the assignment symbol (=) to give variable a value

  • x=17

3
New cards

Adding Variable Values and Viewing the Result

In: x + y

Out: 10

4
New cards

assignment symbol

=

5
New cards

Variable Names

An identifier. May consist of letters, digits and underscores (_) but may not begin with a digit. Python is case sensitive.

6
New cards

A function performs a task when you call it by writing its name, followed by

parentheses ()

7
New cards

type() function

returns the type of the specified object. The parentheses contain the function’s argument—the data that the type function needs to perform its task

8
New cards

/

True division. Divides a numerator by a denominator and yields a floating-point number

9
New cards

//

Floor division. Divides a numerator by a denominator, yielding the highest integer that’s not greater than the result. Rounds down and truncates (discards) the fractional part.

10
New cards

%

Remainder operator. Yields the remainder from division

11
New cards

*

Multiplication operator

12
New cards

**

Exponentiation operator. Raises one value to the power of another

13
New cards

** (0.5)

Calculates square root

14
New cards

ZeroDivisionError

Dividing by zero with / or // is not allowed and results in an exception

15
New cards

NameError

An exception occurs if you try to use a variable that you have not yet created

16
New cards

Algebraic expressions must be typed in

straight-line form using Python’s operators.

17
New cards

Grouping Expressions with Parentheses

Parentheses group Python expressions, as in algebraic expressions

18
New cards

What has the highest level of precedence in Python expressions?

Expressions in parentheses evaluate first, so parentheses may force the order of evaluation to occur in any sequence you desire. Parentheses have the highest level of precedence. In expressions with nested parentheses, such as (a / (b - c)), the expression in the innermost parentheses (that is, b - c) evaluates first.

19
New cards

What operations evaluate after parentheses?

Exponentiation operations evaluate next. If an expression contains several exponentiation operations, Python applies them from right to left.

20
New cards

What operations come after exponentiation?

Multiplication, division and modulus operations evaluate next. If an expression contains several multiplication, true-division, floor-division and modulus operations, Python applies them from left to right. Multiplication, division and modulus are “on the same level of precedence.”

21
New cards

What operations evaluate last?

Addition and subtraction operations evaluate last. If an expression contains several addition and subtraction operations, Python applies them from left to right. Addition and subtraction also have the same level of precedence.

22
New cards

All Python operators of the same precedence group left-to-right except for

the exponentiation operator **, which groups right-to-left.

23
New cards

If both operands are integers,

the result is an integer—except for the true-division ( / ) operator, which always yields a floating-point number.

24
New cards

If both operands are floating-point numbers,

the result is a floating-point number.

25
New cards

Mixed-type (float and integer) expressions produce

floating-point results.

26
New cards

print() function

prints the specified message to the screen

27
New cards

Strings

Can be enclosed in single ‘ ‘ or double “ “ quotes

28
New cards

Printing a Comma-Separated List of Items

Displays each argument separated from the next by a space

In: print(’Welcome’, 'to’, 'Python!')

Out: Welcome to Python!

29
New cards

A backslash ( \ ) in a string is

the escape character. The backslash and the character immediately following it form an escape sequence.

30
New cards

\n

Insert a newline character in a string. When the string is displayed, for each newline, move the screen cursor to the beginning of the next line

31
New cards

\t

Insert a horizontal tab. When the string is displayed, move the screen cursor to the next tab stop for each tab.

32
New cards

\\

Insert a backslash character in a string.

33
New cards

\”

Insert a double quote character in a string.

34
New cards

\’

Insert a single quote character in a string.

35
New cards

Split a long string (or a long statement) over several lines by using

the \ continuation character as the last character on a line to ignore the line break. 

<p>the \ continuation character as the last character on a line to ignore the line break.&nbsp;</p>
36
New cards

Triple-Quoted Strings

‘‘‘ or “““

Used for:

  • multiline strings

  • strings containing single or double quotes

  • docstrings—the recommended way to document the purposes of certain program components.

37
New cards

A string delimited by single quotes

may include double-quote characters, but not single quotes, unless you use the \' escape sequence.

38
New cards

A string delimited by double quotes

may include single quote characters, but not double quotes, unless you use the \" escape sequence.

39
New cards

Triple-quoted strings may contain

both single and double quotes.

40
New cards

Multiline Strings

IPython knows that the string is incomplete because we did not type the closing ,””” before we pressed Enter.

IPython displays a continuation prompt ...: at which you can input the multiline string’s next line.

This continues until you enter the ending """ and press Enter.

Python stores multiline strings with embedded newline characters.

<p>IPython knows that the string is incomplete because we did not type the closing ,””” before we pressed Enter.</p><p>IPython displays a continuation prompt ...: at which you can input the multiline string’s next line.</p><p>This continues until you enter the ending """ and press Enter.</p><p>Python stores multiline strings with embedded newline characters.</p>
41
New cards

input()

requests and obtains user input

42
New cards

input always returns a

string

43
New cards

string concatenation

" " + " "

creates a new string containing the left operand’s value followed by the right operand’s value

44
New cards

Getting an integer from the input

convert the string to an integer using the built-in int function.

Ex: int(input("What is your age?: ")

45
New cards

If the string passed to int cannot be converted to an integer,

a ValueError occurs

46
New cards

int() can also convert

a float into an int

47
New cards

A condition is a Boolean expression with the value

True or False

48
New cards

Comparison operators

  • >

  • <

  • >=

  • <=

all have a higher precedence than

  • ==

  • !=

49
New cards

beginning with the hash character #

indicates that the rest of the line is a comment. You insert comments to document your code and to improve readability. Comments also help other programmers read and understand your code. They do not cause the computer to perform any action when the code executes.

50
New cards

Docstrings

'''  ''' explains the script’s purpose.

Used to describe script components you define, such as new functions and new types called classes

51
New cards

White space

blank lines, space characters, and tab characters

52
New cards

Typically, you write statements on one line. You may spread a lengthy statement over several lines with the

\  continuation character. Python also allows you to split long code lines in parentheses without using continuation characters 

53
New cards

Each if statement consists of the

keyword if, the condition to test, and a colon (:) followed by an indented body called a suite. Each suite must contain one or more statements.

54
New cards

Python requires you to indent the statements in

suites

55
New cards

read == as

“is equal to”

56
New cards

read = as

“is assigned to”

57
New cards

You can chain comparisons to check whether

a value is in a range

Ex: 1<=x<=5

58
New cards

int

Integers

59
New cards

float

Floating-point numbers

60
New cards

str

Strings

61
New cards

Every object has a

type and a value

62
New cards

Assigning an object to a variable

binds (associates) that variable’s name to the object. You can then use the variable in your code to access the object’s value

63
New cards

dynamic typing

it determines the type of the object a variable refers to while executing your code

64
New cards

garbage collection

Python creates objects in memory and removes them from memory as necessary. This ensures that memory is available for new objects you create