Chapter 2: Variables and data types Flashcards

1
Q

create a variable and print the message that assigns a function to capitalize the first letter of each word

A

name = ‘ada lovelace’

print(name.title())

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

define: method in python

A

an action that python can perform on a piece of data

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

print a variable as all uppercase and all lowercase

A

name = ‘ada lovelace’
print(name.upper())
print(name.lower())

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

print a variable but only the first letter of the string should be uppercase

A

name = ‘ada lovelace’

print(name.capitalize())

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

print a first name and last name as separate variables, then combine them to make a complete name. also, make sure the first letters are capitalized

A

first_name = ‘ada’
last_name = ‘lovelace’
full_name = f”{first_name} {last_name}”
print(full_name.title())

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

what is the f in f”{first_name} {last_name}”, and what does it do?

A

the f means format. python uses the f to format the strings by replacing the variable name in the braces with the variable’s value

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

print out a string using the f-string, make sure the name is capitalized

A

print(f”Hello, {full_name.title()}”)

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

assign a f-string to a variable and print that variable out

A

message = f”Hello, {full_name.title()}”

print(message)

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

use the format method to create what is essentially this full_name = f”{first_name} {last_name}”

A

full_name = “{} {}”.format(first_name, last_name)

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

what is whitespace?

A

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

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

add a tab to a string you want to print

A

print(“\tpython”)

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

add new lines to a string that has multiple items

A

print(“languages:\nPython\nC++\nJS”)

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

combine tabs and newlines

A

print(“languages:\n\tPython\n\tC++\n\tJS”)

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

print a variable that has extra whitespace on the right side and get rid of it

A

program = ‘python ‘

print(program.rstrip())

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

permanently delete white space by adding rstrip() to the variable

A
program = 'python '
program = program.rstrip()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

make additional variables with extra whitespace on both sides

A

.rstrip(), .lstrip(), .strip()

17
Q

why is it better to use double quotes instead of single quotes for strings?

A

it is better to use double quotes since you can use apostrophes. in a single quote, you can’t use apostrophes since python wouldn’t know where the string ends

18
Q

add subtract, divide, and multiply integers

A

print(1+1)
print(2-1)
print(1*2)
print(15/3)

19
Q

what in python represents an exponent

A

two of * or **

20
Q

use an exponent to square several numbers

A

print(2**2)

21
Q

what is the name of any number with a decimal in python

A

a Float

22
Q

assign multiple numbers to multiple variables using a single line

A

x, y, z = 0, 0, 0

23
Q

what is a constant in python

A

a variable whose value stays the same throughout the life of the program

24
Q

how is a constant written

A

all caps, MAX_CONNECTIONS = 5000

25
Q

Use a variable to represent your favorite number. Then, using using another variable, create a message that reveals your favorite number. Print that message.

A
fav_num = 42
msg = f"My favorite number is {fav_num}."

print(msg)