Lesson 1 of 0
In Progress

Lesson #2: Variables

In Python, variables are names that can be assigned a value and then used to refer to that value throughout your code.

Python is completely object-oriented, and not “statically typed”. You do not need to declare variables before using them or declare their type.

Creating Variables

To create variables in Python 3, just write:

x = 10
y = “Nick”
z = “Anastasia”
w = 5.3
print(x)
print(y)
print(z)
print(w)

Python Variable Naming Conventions

In many programming languages, it’s common to write variable names in mixedCase. In this system, you capitalize the first letter of every word except the first and leave all other letters in lowercase. For example, numProducts and collectionOfProducts are written in mixedCase.

In Python, however, it’s more common to write variable names in lower_case_with_underscores. In this system, you leave every letter in lowercase and separate each word with an underscore. For instance, both num_products and collection_of_products are written using the lower_case_with_underscores system.

Casting Techniques

If you want to specify the data type of a variable, then you can use casting.

a = str(5)    # a will be ‘5’
y = int(5)    # y will be 5
z = float(5# z will be 5.0

Python allows you to assign values to multiple variables in one line:

name, surname, age = “Nick”, “Rousvelt”, “45”
print(name)
print(surname)
print(age)

Exercises