Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb
Views: 648
Variable Assignment
Rules for variable names
names can not start with a number
names can not contain spaces, use _ intead
names can not contain any of these symbols:
it's considered best practice (PEP8) that names are lowercase with underscores
avoid using Python built-in keywords like
list
andstr
avoid using the single characters
l
(lowercase letter el),O
(uppercase letter oh) andI
(uppercase letter eye) as they can be confused with1
and0
Dynamic Typing
Python uses dynamic typing, meaning you can reassign variables to different data types. This makes Python very flexible in assigning data types; it differs from other languages that are statically typed.
Pros and Cons of Dynamic Typing
Pros of Dynamic Typing
very easy to work with
faster development time
Cons of Dynamic Typing
may result in unexpected bugs!
you need to be aware of
type()
Assigning Variables
Variable assignment follows name = object
, where a single equals sign =
is an assignment operator
Here we assigned the integer object 5
to the variable name a
.
Let's assign a
to something else:
You can now use a
in place of the number 10
:
Reassigning Variables
Python lets you reassign variables with a reference to the same object.
There's actually a shortcut for this. Python lets you add, subtract, multiply and divide numbers with reassignment using +=
, -=
, *=
, and /=
.
Determining variable type with type()
You can check what type of object is assigned to a variable using Python's built-in type()
function. Common data types include:
int (for integer)
float
str (for string)
list
tuple
dict (for dictionary)
set
bool (for Boolean True/False)
Simple Exercise
This shows how variables make calculations more readable and easier to follow.
Great! You should now understand the basics of variable assignment and reassignment in Python.
Up next, we'll learn about strings!