Variables and Data Types
Variables
Variables are used to store information, to be referenced later and used in our programs. Variables label data. They can also be thought of as a container for information. A variable consists of a name, a place inside the memory, and a value.
Note
Every variable is stored somewhere inside the RAM. The name is for the programmers to use so that we could reference that variable, instead of writing
@1002. A variable holds a value, and each value belongs to some type.
Syntax
A variable is created as follows:
variableName = valueWhere variableName is the name of our variable, and the value is the thing that we want the variable to bind to.
Info
This creates a box in memory.
variableNamewill be used as a reference to that spot.variableNamewill bind tovaluei.e. gets assigned to thatvalue.
Naming Rules
Each programming language puts some rules for how the a variable could be named. In Python we have the following rules:
- Start with a letter
[a-zA-Z]or an underscore_, but never a digit. - Followed by any letter, digit or underscore.
- A name should not be a reserved keyword
if,for,while,…
Note
Python variable names are case-sensitive, meaning that
myVaris not the same asmyvar.
Python 3 allows Unicode variable names, however we need to take into consideration the following:
- Allowed characters are the ones that are classified as “like” alphanumeric and underscore.
- Certain Unicode letters are not allowed, such as mathematical operations (+, -,..), emoji, and special characters like
@,#, or spaces
x = 3age = 21num_1 = 30first = -5_second = 13π = 3.1415علي = "Me"It feels weird to have your name in Arabic as a variable name, but it is what it is.
Data Type
In programming a data type is the type of data that can be stored inside a variable. You can think of it as the domain, a set of allowed values that, if a variable possess any, then we say it belongs to that type. We will explore the following types:
intstrfloatNoneboolcomplex
These are called the primitive data types.
Integer
Integer is a non-decimal number. It can be positive, or negative.
x = 10y = -200z = 12345678912345678123456789123456789t = 0Note
The integer implementation in Python has unlimited precision.
String
A string is a sequence of characters enclosed with single quotations ' or double quotations ".
var = "Hey!"courseName = "Python Fundamentals"welcome = 'Good evening'Note
We will have a dedicated section for strings in future lessons.
Float
A floating point number is a number that has a decimal point.
pi = 3.14e = 2.718281sqrt2 = 1.4142135623730951y = 0.0z = -1.23456789123456789Note
In Python,
floats are usually (but not necessarily) implemented using Cdouble.
None
None represents void, no value, or null.
x = Noney = NoneNote
This is mostly used by functions when they don’t return a value.
Boolean
A boolean is a value that can be either True or False. Some values are tested for truthiness or falsiness, using the if statement, while loop, or other control flow statements. By default everything is considered truthy, except for:
- Constants defined to be false:
None,False, - Zero numeric values:
0,0.0,0j - Empty sequences and collections:
'',(),[],{},set(),range(0)
flag = FalseisOn = TrueNote
Booleans are subtype of integers. Objects are considered falsy if they define the method
__bool__that returnsFalseor__len__that returns0. This will be explored in the OOP chapter.
Complex
Complex numbers are numbers that have a real and an imaginary part. Both parts are floating point numbers.
x = 1.23 + 4.56jy = 1.23 - 4.56jz = 2jt = 1 + 0ju = 0jNote
The letter
jis the imaginary unit, it is defined as the square root of -1. It should exist in a complex number, otherwise it will be treated as a floating point number.
Numbers are created by numeric literals or as a result of operations and built-in functions (including their constructors). Numeric literals that contain a decimal point or an exponent are floating point numbers. Adding the j or J suffix yields a complex number.
Numbers
We can group digits in a number using underscores. This is called number literals.
w = 1_000_000_000x = 1_000_000_000.0y = 1_000_000_000.0_123_456_789z = 22_333 + 5_123.6_789jExercises
Exercise 1: Invalid Variable Name
Remove illegal characters from the variable’s name
2m-first_name =42mfirst_name = 42Exercise 2: Variable type
Give the type of each variable
var = 3hello = "123"x = 123y = 3.14z = '2.718281'test = "Something"k = "True"p = False10 = ReadyintstrintfloatstrstrstrboolErrorExercise 3: Spot the mistake
Spot the mistake
V_ar = "Hello World'We are using a double quotation from the left side, but a single quotation from the right side.
