Variables

What is a Variable?

  • A container (like a space ) that is able to hold data, and memorise it when typing its name.

    • Stores, manipulates and displays informations on programs.

  • Each unique variable must have its own name and value.

    • Python can recognize each variable.

To initialize a variable, type as following:

variable_name=value

Valuable numbers can be of different types:

int - whole numbers, no decimals (ex. 1, -12, 20)
float - numbers with decimals (ex. 2.34, 0.0001, -7.5)

Therefore to make a int type of variable, with the given name a and with the value of 2 would be:

a = 2

And to make a float type of variable, with the given name b with the value of 0.75 would be:

b = 0.75

String

  • A char is a single character (ex. 1, b, %, .)

  • The str (string) is a special type made of multiple chars.

Initializing a string value requires that it is enclosed with single or double quotations marks:

s1 = 'one string'
s2 = "two strings"

Boolean

  • A bool (boolean) is a type that only has 2 possible values, True or False.

    • Booleans help create logic within programs, and is how one determines when conditions are met for a code.

Assigning a bool to a value is as follows:

variable_true=True
variable_false=False

Empty Variables

  • None is a value that represents “no value”, aka. nothing. It is a empty variable, that exists. Like a container, with nothing inside of it.

    • None can be used to indicate something was not initialized.

name = None #Name hasn't been entered yet.

The conventions for naming code

  • Naming conventions are guidelines to follow, that makes code more readable and maintable.

    • There are different conventions depending on code language.

  • Variables are written in snake case, aka. they are separated by underscores.

  • It is always important to be descriptive and use meaningful words with variables.

# Bad Naming
isActive = False # not snake case
a = 10
b = "Hello"

# Good Naming
is_active = False
age = 10
greeting = "Hello"