Python Basics Flashcards

1
Q

How do you print “Hello World!” in Python?

A

print(“Hello World!”)

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

What is the ‘console’ in Python?

A

The console displays whatever you put inside print(). The console here is like the Command Line or Terminal on your computer.

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

What is print() in Python? Is it a Function, String, or Variable?

A

print() is a Function in Python

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

What is it called when we put text inside quotes in Python?

A

Any text inside of single or double quotes in Python is called a String. We can use the print Function to display Strings in the Console with Python.

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

We can also combine strings by adding them together:

print (“Hello” + “world!”)

What will the above display in the console when run in Python?

A

It will display: Helloworld!

This is because we forgot to put a space at the end of “Hello” or the beginning of “world!”

Python doesn’t add any whitespace when we combine strings. We have to add it ourselves.

We could also add whitespace this way:

print(“Hello” + “ “ + “world!”)

Hello world!

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

Sometimes it is useful to know the number of characters in a string. What function in Python can we use for this?

A

We can use the function len() for this:

print(len(“Hello world!”))

When run, will display on the console as:

12

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

How can we add the lengths (the number of characters) of two strings together in Python?

A

We can use the function len() in this way:

print(len(“Hello”) + len(“world!”))

When run, will display on the console as:

11

Because we did not put a space between “Hello” and “world!”, the strings are a total of 11 characters in length, instead of 12 with a whitespace.

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

What are strings surrounded by in Python?

A

Single or double quotes

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

How do we display strings to the console in Python?

A

The print() function displays strings to the console.

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

What will this program output?

print(len(“Hel” + “lo!”))

A

When run, this program will display on the console as:

6

Remember, this is because len() counts all the characters in a string, even special characters like “!”.

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

Can we add whitespace to the beginning or end of a string in Python?

A

Yes. For instance:

print(“Hello “ + “world!”)

or

print(“Hello” + “ world!”)

Will both display in the console as:

Hello world!

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

What do variables allows us to do in Python?

A

Variables let us store values in a name. Then we are able to use that name later to refer to the value.

For instance, the following program:

animal = “Lion”
print(animal)

When run, will display in the console as:

Lion

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

Can we combine variables in Python?

Can we print variables just like strings in Python?

A

Yes and yes.

For instance, the following program:

lion = “Lion”
zebra = “Zebra”
print(lion + zebra)

When run, will display in the console as:

LionZebra

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

What do we need to be careful of when using variables in Python?

A

We need to be careful to never use a variable before we define it.

For instance, the following program:

print(animal)
animal = “Zebra”

When run, will display in the console as:

NameError: name ‘animal’ is not defined

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

Can we update variables after we’ve already assigned them a value in Python?

A

Yes. Once we’ve assigned a value to a variable, we can update the variable by setting it to a new value.

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

Can you determine the output of the following program?

animal = “Lion”
animal = “Zebra”
print(animal)

A

When run, it will display in the console as:

Zebra

Because we updated the ‘animal’ variable’s value from “Lion” to “Zebra” before using the print() function.

17
Q

What are some symbols we can’t use alone to name variables in Python?

A

The equals sign, the plus sign, the multiplication or star sign:

= + *

None of these can be used alone to name a variable in Python.

18
Q

Can you determine what the following program will display in the console in Python?

animal = “Tigers”
description = “ are fierce!”
print(animal + description)

A

When run, it will display in the console as:

Tigers are fierce!

Because we named the variables with values before using the print() function.