Python Basics: Variables, Literals and Constants π
Howdy, thanks for checking out this Python beginners article! π If you haven't read Part 1 in this series, you can check it out here!
But, if you've already read Part 1...
LET'S GET ROLLIN'! π
Check out the Table of Contents below to see what we'll be going over in Part 2 β¬οΈ
Table of Contents
Variables
A variable is a named location used to store data in memory. Think of variables like a container that holds data and can be changed later in the program. See below β¬οΈ
item = 200
Above, I created a variable named item
and assigned the numerical value of 200 to it!
I usually think of variables as a grocery basket with items inside and any of the items can be replaced at any time.
We can easily rewrite variable assignments like so β¬οΈ
item = 100
item = 1.1
Initially, the variable item
had a value of 100, but was rewritten to represent the value 1.1
Assigning Values to Variables
Declaring and assigning values to variables is super easy in Python! Here's an example below β¬οΈ
website = "seymourbutts.com"
print(website)
And the output:
seymourbutts.com
I assigned a value seymourbutts.com
to the variable website
. Then, I printed out the value assigned website
i.e. seymourbutts.com!
Now, if you decide you want to change the value of your variable, that is also super easy! Check it out β¬οΈ
# assign initial value to website
website = "seymourbutts.com"
print(website)
# assigning a new value to website
website = "udemy.com"
print(website)
And the output:
udemy.com
But, what if you need to assign multiple values to multiple variables? Look below β¬οΈ
a, b, c = 10, 12.2, "Howdy"
print (a)
print (b)
print (c)
Output:
10
12.2
Howdy
You an also assign the same value to multiple variables!
d = e = f = "same"
print (d)
print (e)
print (f)
Output:
same
same
same
Constants
Constants are a variable, but they cannot be changed. Think about constants like your grocery basket of items, it holds your groceries, which can't be replaced! These are perfect for values that need to be referenced in your code, but you don't plan on changing. π
For example, in a grocery list calculator, a constant would be SALES TAX = 6.25
or in a math calculator program, PI = 3.14
. Constants are usually capitalized to prevent them from getting confused with normal variables.
Here's an example of declaring a constant β¬οΈ
PI = 3.14
Now, let's talk about rules for naming variables & constants!
- Give constants and variables a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_)!
Like so:
snake_case
MACRO_CASE
camelCase
CapWords
- Create a name that makes sense for the program you're creating! Like:
groceries
makes more sense thang
! - Use underscore (_) if you want to create a variable with two or more words! Like:
my_name
orgrocery_list
! - Use capital letters to declare constants. Like:
GRAVITY = 9.7
Literals
Literals are raw data given in a variable or constant. There's numerous types of literals in Python, but don't worry-- we'll break 'em down π
Numeric Literals
Numeric literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types: Integer
(whole numbers) , Float
(decimal numbers), and Complex
(complex numbers are in the form x + yi).
So, how do you use numeric literals in Python? Check it out below β¬οΈ
a = 0b1010 #Binary Literals
b = 1000 #Decimal Literal
c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal
#Float Literal
float_1 = 10.5
float_2 = 1.5e2
print(a, b, c, d)
print(float_1, float_2)
Output:
10 100 200 300
10.5 150.0
Breakdown:
- I assigned integer literals into different variables above. In the first block of code,
a
is a binary literal,b
is a decimal literal,c
is an octal literal andd
is a hexadecimal literal. - In the second block of code commented
#Float Literal
,10.1
and1.5e2
are floating-point literals.1.5e2
is expressed with exponential (thee
stands for exponential) and is equivalent to1.5 * 102
String Literals
A string literal is a line of characters surrounded by quotes. We can use both single, double, or triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes!
So.. how do you use string literals in Python? Check it out below β¬οΈ
strings = "I love Python"
char = "C"
multiline_str = """This is a multiline string with more than one line of code!"""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"look! it's a \n string"
print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
Output:
I love Python
C
This is a multiline string with more than one line of code!
ΓnicΓΆde
look! it's a \n string
Alright, here's the breakdown:
I love Python
is a string literal andC
is a character literal.- The value in triple-quotes """ assigned to the
multiline_str
is a multi-line string literal. - The string
u"\u00dcnic\u00f6de"
is a Unicode literal which supports characters other than English. In this case,\u00dc
representsΓ
and\u00f6
representsΓΆ
.
Hold on, folks...we're almost done with Literals!
Boolean Literals
These are simple (thank the heavens!): A Boolean literal can have any of the two values, True
or False
.
Most importantly! True
= the value of 1
and False
= the value of 0.
How do we use boolean literals in Python? Check it out β¬οΈ
x = (1 == True)
y = (9 == False)
a = True + 5
b = False + 10
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
Output:
x is True
y is False
a: 6
b: 10
Breakdown:
In the code above, I used a boolean literal
True
andFalse
. (Don't forget: in Python, True equals the value 1 and False equals 0.) The value ofx
isTrue
because 1 is equal to True. And, the value ofy
isFalse
because 1 is not equal to False.Similarly, I used
True
andFalse
with numeric expressions as their value. The value ofa
is5
because we add True which has a value of 1 with 4. Andb
is10
because we add False, which has a value of 0, to 10.
Special Literals
This one is fun! π Python contains one special literal --> None
. We use it to specify that the field haven't been created yet! Check out an example β¬οΈ
drink = "Available"
appetizer = None
def menu(x):
if x == drink:
print(drink)
else:
print(appetizer)
menu(drink)
menu(appetizer)
Output:
Available
None
Breakdown:
- I defined a
menu
function. Insidemenu
, when we set the argument as drink then, it displaysAvailable
. And, whenmenu()
's argument isappetizer
, it displaysNone
!
You're done!! ππ
In Part 3, I'll be going over:
- All the Python Operators (arithmetic, logical, assignment operators)
- Input, Output and Import (how to take input from the user, save it to a variable, display output, and import other Python modules) π
We're soo close to writing our first block of Python code!!!! Whoooop!! π See you next week β€οΈ