Python Crash Course: A Hands-On, Project-Based Introduction to Programming

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/159

flashcard set

Earn XP

Description and Tags

Book by Eric Matthes

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

160 Terms

1
New cards

print()

prints to the screen whatever is inside the parentheses

2
New cards

value

information associated with a variable

3
New cards

Variable names can contain only

letters, numbers, and underscores.

4
New cards

Variables can start with a letter or an underscore, but not

with numbers

5
New cards

​​Spaces are not allowed in variable names, but

underscores can be used to separate words in variable names

6
New cards

String

a series of characters. Anything inside quotes. You can use single or double quotes around them

7
New cards

Method

an action that Python can perform on a piece of data. Every method is followed by a set of parentheses, because methods often need additional information to do their work

8
New cards

.lower()

convert strings to lowercase. Temporary. Doesn’t change the value that was originally stored

9
New cards

.title()

changes each word to title case, where each word begins with a capital letter

10
New cards

.upper()

converts strings to uppercase

11
New cards

f-­strings

f is for format, because Python formats the string by replacing the name of any variable in braces with its value

12
New cards

To insert a variable’s value into a string,

place the letter f immediately before the opening quotation mark. Put braces around the name or names of any variable you want to use inside the string. Python will replace each variable with its value when the string is displayed.

13
New cards

.format()

list the variables you want to use in the string inside the parentheses following format. Each variable is referred to by a set of braces; the braces will be filled by the values listed in parentheses in the order provided

14
New cards

Whitespace

any nonprinting character, such as spaces, tabs, and end-­ of-­line symbols

15
New cards

\t

to add a tab to your text

16
New cards

\n

to add a newline in a string

17
New cards

\n\t

move to a new line, and start the next line with a tab

18
New cards

.rstrip()

to ensure that no whitespace exists at the right end of a string

19
New cards

associate the stripped value with the variable name

to remove the whitespace from the string permanently

20
New cards

.lstrip()

strip whitespace from the left side of a string

21
New cards

.strip()

strip whitespace from both sides at once

22
New cards

**

exponents

23
New cards

/

float division

24
New cards

//

integer division

25
New cards

Python supports the order of operations too, so

you can use multiple operations in one expression. You can also use parentheses to modify the order of operations so Python can evaluate your expression in the order you specify.

26
New cards

Float

any number with a decimal point

27
New cards

When you divide any two numbers, even if they are integers that result in a whole number,

you’ll always get a float

28
New cards

When you’re writing long numbers, you can group digits using

underscores to make large numbers more readable for you. Python ignores the underscores when storing these kinds of ­ values

29
New cards

You can assign values to more than one variable using

just a single line. You’ll use this technique most often when initializing a set of numbers. You need to separate the variable names with commas, and do the same with the values, and Python will assign each value to its respectively positioned variable. As long as the number of values matches the number of variables, Python will match them up correctly

30
New cards

Constant

a variable whose value stays the same throughout the life of a program. Use all capital letters to indicate a variable should be treated as a constant and never be changed

31
New cards

#

a comment  to explain what your code is supposed to do and how you are making it work

32
New cards

List

a collection of items in a particular order. Mutable. Wrapped in square brackets []. Individual elements are separated by commas.

33
New cards

Access any element in a list by

writing the name of the list followed by the index of the item enclosed in square brackets

34
New cards

Index of first item in a list

[0]

35
New cards

Index of last item in a list

[-1]

36
New cards

you can also use f-strings to create a message based on

a value from a list

37
New cards

To change an element in a list

use the name of the list followed by the index of the element you want to change, and then = provide the new value you want that item to have.

38
New cards

.append()

The new element in the () is added to the end of the list

39
New cards

you can start with an empty list and then add items to the list using

a series of append() calls

40
New cards

.insert( , )

Add a new element at any position in your list. In the (), specify the index of the new element and the value of the new item. This operation shifts every other value in the list one position to the right

41
New cards

del statement

Use if you know the index of the item you want to remove from a list. You can no longer access the value that was removed

42
New cards

.pop()

method removes the last item in a list, but it lets you work with that item after removing it. To remove an item from any position in a list, include the index of the item you want to remove in ()

43
New cards

.remove()

Use this method if only know the value of the item (you don’t know the index) you want to remove. Can still work with value after it’s removed. deletes only the first occurrence of the value you specify. If there’s a possibility the value appears more than once in the list, you’ll need to use a loop to make sure all occurrences of the value are removed.

44
New cards

.sort()

permanently change the order of the list to store them alphabetically or in ascending order

45
New cards

.sort(reverse=True)

permanently change the order of the list to store them in reverse alphabetical or in descending order

46
New cards

sorted()

maintain the original order of a list but present it in a sorted order. List variable in (). Also accepts reverse=True

47
New cards

.reverse()

permanently reverses the original order of a list. Can revert to the original order anytime by applying the method to the same list a second time

48
New cards

len()

find the length

49
New cards

Index error

Python can’t find an item at the index you requested

50
New cards

for loop

takes a collection of items and executes a block of code once for each item in the collection. You know the number of iterations

51
New cards

looping

set of steps is repeated once for each item in the list, no matter how many items are in an iterable. each indented line is executed once for each value. Any lines of code after the loop that are not indented are executed once without repetition

52
New cards

inside the loop

Every indented line (body) following the head. Each indented line is executed once for each value

53
New cards

only lines you should indent are

the actions you want to repeat for each item in a for or while loop

54
New cards

range() function

makes it easy to generate a series of numbers.

55
New cards

range(start, stop+1, step)

  • If step is excluded, default increasing by 1

    • Two numbers are (start, end+1)

  • If start is excluded, 0 is default start

    • One number (end+1)

  • Get even numbers: 2 as step

56
New cards

list(range())

convert the results of range() directly into a list of numbers

57
New cards

list comprehension

combines the for loop and the creation of new elements into one line, and automatically appends each new element

58
New cards

how to write a list comprehension

begin with a descriptive name for the list. Next, open a set of square brackets and define the expression for the values you want to store in the new list. Then, write a for loop to generate the numbers you want to feed into the expression, and close the square brackets. No colon is used at the end of the for statement.

59
New cards

slice

work with a specific group of items in a list. Specify the index of the first and last elements you want to work with

60
New cards

listname[start:end+1:step]

  • If you omit the first index in a slice, Python automatically starts your slice at the beginning of the list [:2]

  • Omit the end index, it will default get all the elements since the indicated start. Like [2:]

  • [:] get all elements

61
New cards

You can use a slice in a for loop if

you want to loop through a subset of the elements in a list

62
New cards

copy the entire list using slicing

by omitting the first index and the second index ([:])

63
New cards

tuple

list-like. Immutable and elements put in ( ). Index to access elements. If you want to define a tuple with one element, you need to include a trailing comma (3,)

64
New cards

immutable

cannot change

65
New cards

mutable

can change

66
New cards

Although you can’t modify a tuple, you can

reassign by associating a new tuple with the variable

67
New cards

conditional test (Booleon expression)

every if statement is an expression that can be evaluated as True or False. Python uses the values True and

False to decide whether the code in an if statement should be executed.

  • If a conditional test evaluates to True, Python executes the code following the if statement.

  • If the test evaluates to False, Python ignores the code following the if statement.

68
New cards

== double equal sign

equality operator returns True if the values on the left and right side of the operator match, and False if they don’t match

69
New cards

variable = value

states “set the variable equal to value”

70
New cards

variable == value

asks “Is the variable equal to value?”

71
New cards

two values with different capitalization are

not considered equal

72
New cards

!=

determine whether two values are not equal. The exclamation point represents not. If these two values do not match, Python returns True and executes the code following the if statement. If the two values match, Python returns False and does not run the code following the if statement.

73
New cards

To check whether two conditions are both True simultaneously

use the keyword and to combine the two conditional tests;

  • If each test passes, the overall expression evaluates to True.

  • If either test fails or if both tests fail, the expression evaluates to False

74
New cards

The keyword or allows you to check multiple conditions as well, but

it passes when either or both of the individual tests pass. An or expression fails only when both individual tests fail.

75
New cards

Use the keyword in

To find out whether a particular value is already in an iterable and to iterate through a sequence in a for loop:.

  • i.e., requested_toppings = ['mushrooms', 'onions', 'pineapple']

    'mushrooms' in requested_toppings

    True

76
New cards

Use the keyword not in

to know if a value does not appear in an iterable

77
New cards

Boolean value

either True or False, just like the value

78
New cards

simple if statement

if conditional_test:

do something

You can put any conditional test in the first line and just about any action in the indented block following the test. If the conditional test evaluates to True, Python executes the indented code following the if statement. If the test evaluates to False, Python ignores the indented code following the if statement.

79
New cards

else block

matches any condition that wasn’t matched by a specific if or elif test

80
New cards

In a simple if-else chain,

one of the two actions will always be executed.

81
New cards

if-elif-else chain

Used to test more than two possible situations, and to evaluate. Python executes only one block in the chain. It runs each conditional test in order until one passes. When a test passes for one condition, the code following that test is executed and Python skips the rest of the tests.

82
New cards

elif block

like another if test. It only runs if the previous tests failed. You can use as many elif blocks in your code as you like

83
New cards

is an else block required at the end of an if-elif chain?

not always required. Sometimes an else block is useful; sometimes it is clearer to use an additional elif statement that catches the specific condition of interest. If you have a specific final condition you are testing for, consider using a final elif block and omit the else block

84
New cards

When more than one condition could be True, use

a series of independent if statements with no elif or else blocks. More than one block of code is run regardless of whether the previous test passed or not

85
New cards

Check whether a list is empty by

running a for loop. When only the name of a list is used in an if statement, Python returns True if the list contains at least one item; an empty list evaluates to False

86
New cards

Dictionary

a collection of key-value pairs. Wrapped in braces, {}, with a series of key-value pairs inside the braces. Every key is connected to its value by a colon, and individual key-value pairs are separated by commas.

87
New cards

To get the value associated with a key,

give the name of the dictionary and then place the key inside a set of square brackets []. dictionaryname[key]

88
New cards

Key-value pair

a set of values associated with each other

89
New cards

When you provide a key,

Python returns the value associated with that key.

90
New cards

To add a new key-value pair,

you would give the name of the dictionary followed by the new key in square brackets = along with the new value

91
New cards

To start filling an empty dictionary,

define a dictionary with an empty set of braces and then add each key-value pair on its own line

92
New cards

To modify a value in a dictionary,

give the name of the dictionary with the key in square brackets and then the new value you want associated with that key

93
New cards

Use the del statement in a dictionary

to permanently remove a key-value pair. All del needs is the name of the dictionary and the key that you want to remove.

94
New cards

When you know you’ll need more than one line to define a dictionary,

press enter after the opening brace. Then indent the next line one level (four spaces), and write the first key-value pair, followed by a comma. From this point forward when you press enter, your text editor should automatically indent all subsequent key-value pairs to match the first key-value pair. Once you’ve finished defining the dictionary, add a closing brace on a new line after the last key-value pair and indent it one level so it aligns with the keys in the dictionary. It’s good practice to include a comma after the last key-value pair as well, so you’re ready to add a new key-value pair on the next line.

95
New cards

Using keys in square brackets to retrieve the value you’re interested in from a dictionary might cause one potential problem

if the key you ask for doesn’t exist, you’ll get an error

96
New cards

For dictionaries, specifically, you can use the get() method to

set a default value that will be returned if the requested key doesn’t exist.

97
New cards

.get() for dictionaries

requires a key as a first argument. As a second optional argument, you can pass the value to be returned if the key doesn’t exist. If you leave out the second argument in the call to get() and the key doesn’t exist, Python will return the value None.

98
New cards

None

means “no value exists.” This is not an error: it’s a special value meant to indicate the absence of a value.

99
New cards

Default behavior when looping through a dictionary

loops through keys

100
New cards

Loop through a dictionary with a

for loop