Variables Flashcards

(40 cards)

1
Q

What are variables?

A

Variables are how we store data as our program runs.
- A “variable” is just a name that we give to a value.
- Variables are called “variables” because they can hold any value and that value can change (it varies).

  • For example, we can make a new variable named my_height and set its value to 100:
    my_height = 100
  • Or we can define a variable called my_name and set it to the text string “Lane”:
    my_name = “Lane”

We have the freedom to choose any name for our variables, but they should be descriptive and consist of a single “token”, meaning continuous text with underscores separating the words.

  • Once we have a variable, we can access its value by using its name. For example, this will print 100:
    print(my_height)

And this will print Lane:
print(my_name)

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

What is Addition?

A

+
summation = a + b

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

What is Subtraction?

A

-
difference = a - b

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

What is Multiplication?

A

*
product = a * b

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

What is Division?

A

/
quotient = a / b

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

What is Parentheses?

A

Can be used to order math operations
Example:
avg = (a + b + c) / 3

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

What are Negative Numbers?

A

Negative numbers in Python work the way you probably expect. Just add a minus sign:
my_negative_num = -1

Example Use: When our hero walks through poison, their health should go down. Right now the hero is gaining 10 health instead of losing 10 health. Change the poison_damage variable to be negative.

player_health = 100
poison_damage = -10
player_poison_health = player_health + poison_damage
print(player_poison_health)

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

What are Comments?

A

Adding comments to your code, they are ignored by the Python interpreter.
Useful to add notes, especially when collaborating.

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

How to make a Single Line Comment?

A

A single # makes the rest of the line a comment:

speed describes how fast the player

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

How to make a Multi-Line Comment? AKA Docstrings

A

You can use triple quotes to start and end multi-line comments as well:

Example:
“””
the code found below
will print ‘Hello, World!’ to the console
“””
print(“Hello, World!”)

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

What are Docstrings?

A

Docstrings are multi-line comments.

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

If what the Docstrings and Multi-line Comments do are the same, what makes them different?

A

On average, the term docstrings is said more often just because of how often functions/modules/classes etc. are referenced

The term Docstrings would be used for documentation of the code.
Whereas multi-line comments are used to explain your code either as a reminder to yourself or for peers.

Think of Docstrings as a sort of page divider that you would use in a notebook, a way of marking parts of your code/program.
and Multi-line comments are just sticky notes
————
I’d tackle this as writing for two different audiences who won’t always overlap.

First there are the docstrings; these are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. Consider the official Python documentation - What’s available in each library and how to use it, no implementation details (Unless they directly relate to use)

Secondly there are in-code comments; these are to explain what is going on to people (generally you!) who want to extend the code. These will not normally be turned into documentation as they are really about the code itself rather than usage. Now there are about as many opinions on what makes for good comments (or lack thereof) as there are programmers. My personal rules of thumb for adding comments are to explain:

  • Parts of the code that are necessarily complex. (Optimisation comes to mind)
  • Workarounds for code you don’t have control over, that may otherwise appear illogical
  • I’ll admit to TODOs as well, though I try to keep that to a minimum
  • Where I’ve made a choice of a simpler algorithm where a better performing (but more complex) option can go if performance in that section later becomes critical

If you type help(<function>) Python will return the docstring text, but not your comments. It seems like this is the fundamental answer to your question. The other answers are helpful and nuanced, but I submit that what you were looking for is this.</function>

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

What style of writing should we use for Variable Names?

A

Snake Case : All words are lowercase and separated by underscores

Example code:
my_hero_health

To be clear, your Python code will still work with Camel Case or Pascal Case, but for consistency, it is best to stick with what the creator of Python (Guido van Rossum) implores us to use.

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

What is Snake Case?

A

All words are lowercase and separated by underscores

Example code:
my_hero_health

Language(s) that recommend it:
Python, Ruby, Rust

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

What is Camel Case?

A

Capitalize the first letter of each word except the first one

Example code:
myHeroHealth

Language(s) that recommend it:
JavaScript, Java

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

What is Pascal Case?

A

Capitalize the first letter of each word

Example code:
MyHeroHealth

Language(s) that recommend it:
C#, C++

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

What is No Casing?

A

DO NOT DO THIS, no program language uses this.
All lowercase with no separation

Example code:
myherohealth

18
Q

What are some basic data types for Python?

A
  • Strings
  • Numbers (Integers and Float)
  • Float
  • Booleans
19
Q

What are Strings?

A

In programming, snippets of text are called “strings”. They’re lists of characters strung together. We create strings by wrapping the text in single quotes or double quotes. That said, double quotes are preferred.

Example code:
name_with_single_quotes = ‘boot.dev’ # not so good
name_with_double_quotes = “boot.dev” # so good

20
Q

Why are “Double Quotes” preferred over ‘Single Quotes’ ?

A

According to PEP 8(Python Coding standard)

In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability. For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in PEP 257.

‘single-quote’ is the same as “single-quote” You will need a double quote when you need apostrophe in the middle of the string
EXAMPLE : “I’m a good boy”

21
Q

What is an Integer?

A

An integer (or “int”) is a number without a decimal part:

Example:
x = 5 # positive integer
y = -5 # negative integer

22
Q

What is an Int?

A

A shorthand for integer.

23
Q

What is a Float?

A

A float is a number with a decimal part:
x = 5.2
y = -5.2

24
Q

What is a Boolean?

A

A “Boolean” (or “bool”) is a type that can only have one of two values: True or False.

Example:
is_tall = True
is_short = False

25
What is a Bool?
A shorthand for boolean.
26
What are NoneType variables?
We can make an "empty" variable by setting it to None. None is a special value in Python that represents the absence of a value. It is not the same as zero, False, or an empty string. Example: my_mental_acuity = None The value of my_mental_acuity in this case is None until we use the assignment operator, =, to give it a value. NoneType is not the same as a string with a value of "None": Example: my_none = None # this is a None-type my_none = "None" # this is a string with the value "None"
27
When might you use a None value?
Set None as the default value that will be replaced later. One usecase is to represent that a value hasn't been determined yet, for example, an uncaptured input. For example, maybe your program is waiting for a user to enter their name. You might start with a variable: username = None Then later in the code, once the user has entered their name, you can assign it to the username variable: username = input("What's your name? ") Remember, it's crucial to recognize that None is not the same as the string "None". They look the same when printed to the console, but they are different data types. If you use "None" instead of None, you will end up with code that looks correct when it's printed but fails the tests.
28
Is changing the type of a variable generally a good idea?
No You technically can, but it's not a good idea because then you may have to change multiple things.
29
What Is Non-Dynamic Typing?
Languages that aren't dynamically typed are Statically Typed. In a statically typed language, if you try to assign a value to a variable of the wrong type, an error would crash the program.
30
What is Dynamic Typing?
Python is dynamically typed, which means a variable can store any type, and that type can change. For example, if I make a number variable, I can later change that variable to a string: speed = 5 speed = "five" HOWEVER In almost all circumstances, it's a bad idea to change the type of a variable. The "proper" thing to do is to just create a new one. For example: speed = 5 speed_description = "five"
31
What kind of typing does Python employ?
Dynamic
32
What is a Concatenation?
When working with strings the + operator performs a "concatenation", which is a fancy word that means "joining two strings". Generally speaking, it's better to use string interpolation with f-strings over + concatenation.
33
What do F Strings do?
Lets you combine text strings with variables. You have 10 bananas. we can create strings that contain dynamic values with the f-string syntax Example: num_bananas = 10 f_string = f"You have {num_bananas} bananas" print(f_string) ------------- - Opening quotes must be preceded by an f. - Variables within curly brackets have their values "interpolated" (injected) into the string. - It's just a string with a special syntax. Another Example: aname = "Yarl" age = 37 race = "dwarf" print(f"{name} is a {race} who is {age} years old.") #Yarl is a dwarf who is 37 years old.
34
Generally speaking, is it better to use string interpolation with F Strings over + Concatenation?
Yes, it is better to use string interpolation with F Strings over + Concatenation. Readability and have it more condensed to together rather than making multiple lines of concatenation.
35
How to put an extra space in between different values in strings?
Notice the extra space at the end of "Lane " in the first_name variable. That extra space is there to separate the words in the final result: "Lane Wagner". Extra Space Example: first_name = "Lane " last_name = "Wagner" full_name = first_name + last_name print(full_name) full_name now holds the value "Lane Wagner".
36
How many variables can be declared on the same line?
There is no limit to how many you can declare.
37
What does Multi-Variable Declaration look like and why is it better?
We can save space when creating many new variables by declaring them on the same line: sword_name, sword_damage, sword_length = "Excalibur", 10, 200 Which is the same as: sword_name = "Excalibur" sword_damage = 10 sword_length = 200 Any number of variables can be declared on the same line, and variables declared on the same line should be related to one another in some way so that the code remains easy to understand.
38
What is Clean Code?
Clean Code is code that is easy for developers to read and understand.
39
What is another word for Average?
Mean
40
How is the Average or "Mean" calculated?
The average (or "mean") is calculated by adding up all the numbers and dividing by how many numbers there are. Which means first we need to add up all the scores, then divide by 4. Example: game_one_score = 97 game_two_score = 92 game_three_score = 106 game_four_score = 105 average_score = (97 + 92 + 106 + 105) / 4 print(round(average_score))