Variables Flashcards
(40 cards)
What are variables?
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)
What is Addition?
+
summation = a + b
What is Subtraction?
-
difference = a - b
What is Multiplication?
*
product = a * b
What is Division?
/
quotient = a / b
What is Parentheses?
Can be used to order math operations
Example:
avg = (a + b + c) / 3
What are Negative Numbers?
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)
What are Comments?
Adding comments to your code, they are ignored by the Python interpreter.
Useful to add notes, especially when collaborating.
How to make a Single Line Comment?
A single # makes the rest of the line a comment:
speed describes how fast the player
How to make a Multi-Line Comment? AKA Docstrings
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!”)
What are Docstrings?
Docstrings are multi-line comments.
If what the Docstrings and Multi-line Comments do are the same, what makes them different?
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>
What style of writing should we use for Variable Names?
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.
What is Snake Case?
All words are lowercase and separated by underscores
Example code:
my_hero_health
Language(s) that recommend it:
Python, Ruby, Rust
What is Camel Case?
Capitalize the first letter of each word except the first one
Example code:
myHeroHealth
Language(s) that recommend it:
JavaScript, Java
What is Pascal Case?
Capitalize the first letter of each word
Example code:
MyHeroHealth
Language(s) that recommend it:
C#, C++
What is No Casing?
DO NOT DO THIS, no program language uses this.
All lowercase with no separation
Example code:
myherohealth
What are some basic data types for Python?
- Strings
- Numbers (Integers and Float)
- Float
- Booleans
What are Strings?
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
Why are “Double Quotes” preferred over ‘Single Quotes’ ?
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”
What is an Integer?
An integer (or “int”) is a number without a decimal part:
Example:
x = 5 # positive integer
y = -5 # negative integer
What is an Int?
A shorthand for integer.
What is a Float?
A float is a number with a decimal part:
x = 5.2
y = -5.2
What is a Boolean?
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