aDa LoVeLaCe
[vocab] variable | method
Published in
3 min readJan 17, 2018
→ Lab Assignment: “Changing Case (Methods)” on repl.it
[python learned]
- how to define a variable
.swapcase()
method.title()
method.upper()
method.lower()
method
Using variables & methods
1. Search for “IDLE” in the Start Menu & click to open it.
IDLE is a development environment for Python. It has two main window types, the Shell window and the Editor window. For this example, we will be using the Shell window:
>>>
These three arrows are called the prompt. The prompt automatically appears when the shell is ready to accept commands.
2. Run each line below, one at a time. Press Enter/Return to run each line after typing it.
Remember: You don’t need to type >>>
(the prompt)
>>> name = "aDa LoVeLaCe"
>>> name.swapcase()
- The first line creates a variable. A variable is used to store information (data) so it can be easily used and referenced later. In this case, our variable’s name is
name
and it contains the string"aDa LoVeLaCe"
. - The second line applies the
.swapcase()
method toname
. A method is a function that acts on a specific object — in this case,name
. What does the.swapcase()
method do toname
?
Why use variables?
Variables allow you to use the same value in many places, making it easier to to change the value later. Now if you want to change the value, you only need to do it in one place.
name = "Rosie"print("Hi " + name)
print("Nice to meet you " + name + "!")
print("How are you " + name + "?")
Naming variables
Variable names cannot:
- have spaces
- start with a number
- contain symbols other than
_
(underscore)
# bad variable namesmy number = 3
1st_line = "In a galaxy far, far away..."
kebab-case = "this-is-kebab-case"
score! = 96
Variable names should not:
- be the same as an existing function or keyword (such as
print
), because that gets confusing - start with a capital letter, because that’s reserved for another data type (called “classes,” which we’ll learn about later)
If a variable name has multiple words, it can be written in “camel case” or “snake case”:
# thisIsCamelCase
# thisIsAlsoCamelCase
# canYouSeeWhyThisIsCalledCamelCase# this_is_snake_case
# this_is_also_snake_case
# can_you_see_why_this_is_called_snake_case
Ada Lovelace, from The Thrilling Adventures of Lovelace and Babbage: The (Mostly) True Story of the First Computer, a graphic novel.