How Variables work in Python Flashcards
(21 cards)
What is a variable?
A variable is a container for the value it behaves as the value it contains. Think of variables like in algebra
What are boolean variables?
True or false
How do you create a variable?
Every variable needs a unique name.
I.e. name=”Meeko”
*don’t forget the quotation marks. Code will not run if you don’t hit RUN
Print a variable name of Kevin.
FILE—>NEW—>(name it: Try)–>CLICK PYTHON FILE
NAME=”Kevin”
print (NAME)
Terminal Window: Kevin
*don’t need quotes in PRINT because if you do, the terminal window will display as “Kevin” instead of just Kevin.
What is a string?
A string is a type of variable that is a series of characters.
Create a string literal that reads Goodbye HCS
NAME=”HCS”
PRINT(“Goodbye “+name)
Window Terminal: Goodbye HCS
How do you check the type of a variable?
NAME= “Dog”
PRINT(TYPE(NAME))
TERMINAL WINDOW
What is str?
Str stands for strings which is a series of characters. it is a data type of a variable.
Can you combine variables?
Yes you can combine variables as long as they are of the same data type.
combine variables to display a printed I love Kevin Wallace
FIRST_NAME = “KEVIN”
LAST_NAME = “WALLACE”
FULL_NAME = FIRST_NAME + LAST_NAME
PRINT(“I LOVE “+ FULL_NAME)
T.W.D.: I LOVE KEVINWALLACE
To add a space, go to FULL_NAME = FIRST_NAME +
“ “ + LAST_NAME
T.W.D.: I LOVE KEVIN WALLACE
How do I change the variables to a comment data type?
Add # in front of each line.
What is int?
int is another data type known as an integer. An integer is any whole number.
What happens if you put an int value in “ “?
This changes the variable data type from an int to a str
integer to string, strings cannot be used in a mathematical data types
Increase the age of 52 by 4
Use both ways
AGE = 52 AGE = 52 +4
TWD: 56
AGE = 52
AGE +=4
TWD: 56
Create a code that says Your age is 85 by starting with the original age of 80.
AGE = 80
AGE += 5
PRINT (“Your age is: “ + str(age))
TWD: Your age is 85
What is a float data type?
A float data type is a floating point number. meaning it has a decimal number.
Typecast Your height is 152.25 cm
HEIGHT= 152.25
PRINT(“YOUR HEIGHT IS: “ + STR(HEIGHT) +” “ +”cm”)
TWD: Your height is 152.25 cm
What is a boolean (bool) data type?
Boolean data types or bool can only store true or false.
Create a code that displays Does Kevin Love Meeko: True
ANSWER= True
PRINT(“DOES KEVIN LOVE MEEKO: “ + STR(ANSWER))
TWD Does Kevin love Meeko: True
human= false
print (“Are you human: “ + str(human))
TWD error
Why didn’t the code work?
For Bool, True or False must have correct uppercase.
fix:
human= False
print (“Are you human: “ + str(human))
TWD Are you human: false
What are the 4 basic types of data for Python?
Str (strings): stores a series of characters
Int (integer): stores whole numbers
Float (Floating point number): stores decimal numbers
Bool (Boolean): stores True or False for if statements