fbpx

Variable Types

What are variables?

In Python, a variable is a named reference to a value stored in the computer’s memory. It allows you to store and manipulate data in your program.

To create a variable in Python, you simply choose a name for the variable and assign a value to it using the equal sign (=).

There are a variety of different types of variables we can store, from text, to numbers, to lists.

Types of variables in Python

Strings

A string is a sequence of characters enclosed in single quotes, double quotes, or triple quotes. It can contain letters, numbers, symbols, and whitespace.

For example:

				
					greeting = 'Hello, World!'

				
			

In the above example, greeting is a string containing the characters “Hello, World!”.

Strings in Python are immutable, which means that once they are created, they cannot be changed. However, you can create new strings by concatenating or manipulating existing ones.

For example:

				
					greeting = 'Hello, ' + 'World!'
				
			

Python provides many built-in functions and methods for working with strings, such as len(), upper(), lower(), replace(), split(), and join(). We will dive deeper into these later on.

Integers

An integer is a numeric data type that represents a whole number. It can be a positive number, a negative number, or zero. Integers in Python have unlimited precision, which means they can represent arbitrarily large numbers as long as there is enough memory available.

You can create an integer in Python by simply assigning a whole number to a variable, like this:

				
					score = 25
				
			

In this example, score is assigned the integer value 25. You can perform arithmetic operations on integers just like you would with regular numbers, using the standard mathematical operators (+, -, *, /, %, //, **). We will touch on these more shortly.

Floats

A float is a data type used to represent decimal numbers. Floats are used to represent numbers with fractional parts, such as 3.14 or 2.71828.

You can create a float in Python by including a decimal point in a numerical value. For example:

				
					pi = 3.14
e = 2.71828
GPA = 3.57
price = 29.99
				
			

Python supports all the standard mathematical operations on floats, such as addition, subtraction, multiplication, and division. However, due to the way floating-point numbers are represented in computers, some operations can result in small inaccuracies, which can accumulate over time. Therefore, it’s important to be aware of the limitations of floating-point arithmetic when working with floats in Python.

Python provides many built-in functions for working with floats, such as round(), abs(), and math.sqrt(). For example, the round() function can be used to round a float to a specified number of decimal places, as follows:

				
					x = 3.14159
y = round(x, 2)   # Rounds x to 2 decimal places (3.14)
				
			

Booleans

A boolean is a data type used to represent the truth values True and False. Booleans are used to represent logical values, such as whether a condition is true or false, whether a response is yes or no, or whether a statement is valid or invalid.

You can create a boolean in Python using the keywords True and False, for example:

				
					x = True
y = False
				
			

Sometimes, it may also be easier to think about booleans as an individual bit of data (True = 1, False = 0), as this is how they are stored in memory at the lowest level.

Boolean variables can also be created by evaluating an expression that returns a boolean value, such as expressions that use comparison operators (>, <, ==, etc). We will interact with in detail when we get to conditionals.

More Notes on Variables

Python is a dynamically-typed language, which means that you don’t have to declare the type of a variable before using it. The type of a variable is determined by the value it holds. For example, if we assign a string value to a variable, it becomes a string variable, and if we assign a numerical value to a variable, it becomes a numerical variable. While Python is very flexible in this way, other languages such as Java and C++ have much more rigid syntax for creating variables and modifying their types.

It’s important to note that variables are case-sensitive in Python, meaning that age and AGE are different variables.

Additionally, variable names must not contain any spaces or dashes (-). For example, last name and last-name are invalid variable names, however lastName and last_name would be valid.

Python also allows you to assign multiple variables in a single line, as follows:

				
					x, y, z = 10, 20, 30

				
			

In the above example, we create three variables named x, y, and z, and assign them the values 10, 20, and 30, respectively.

Replit Practice

Loading...