Skip to main content

Variables and Data Types

Introduction to Variables

Let's create a new Python file called variables by again right-clicking first-pycharm-project and clicking on New > Python file. Like before, we'll enter the desired name in the window that pops up. I have chosen the name variables.py.

Now with our blank variables.py file we can begin to learn what variables are about.

Why Use Variables?

Python allows us to perform calculators with whole-numbers. In programming, we refer to these whole-number values as integers or ints.

Let's say we want to use Python to add two whole-numbers. We can use Python as a calculator. Try entering the following code in your variables.py file and running it.

print(1 + 1)

As you'd expect, Python tells us that 1 + 1 gives a result of 2.

one-plus-one-print.png

However, rather than simply print of such values once, we may want to store the results of a more complex calculation so that it can be used or modified later. This is when we would use something called a variable.

What Are Variables?

In the world of programming, variables are like containers that hold information. Imagine them as labeled boxes where you can store different types of data. These data could be numbers, words, sentences, or anything else your program needs to work with.

Variables allow you to give a name to a piece of data, making our code more readable and organised.

Naming Variables

Just like how a recipe name should be meaningful, variable names should also be descriptive. Variable names start with a letter or underscore (_) and can be followed by letters, numbers, or underscores. However, they can't start with a number or contain spaces or special characters. For example, recipe_name or ingredient_count are good variable names. */

Assigning ValuesAssignment

To put data into a variable, we use the assignment operator =. Let'sNow saywe youcan wantfind toour 1 + 1 and store the number of ingredients needed for a cake:result.

ingredient_countmy_sum = 101 + 1

Here, ingredient_count is the variable name, and 10 is the value assigned to it.

Reusing Variables

Variables can hold different values over time. If you later find out you need 12 ingredients for the cake, you can simply change the value:

ingredient_count = 12

No need to create a new variable; the old one updates with the new value.

Dynamic Nature of Variables

Unlike some other programming languages, Python is dynamically typed. This means you don't have to specify the type of data a variable will hold. Python figures it out on its own!

Example: Storing a Name

Let's see a quick example. We can use a variable to store a person's name:

name = "Alice"

Now the variable name contains the string "Alice". You can useBut this variablealone towon't display the nameresult orwhen manipulatewe run the program. In order to this we will use another print() statement, but give it inour variousmy_sum ways.variable.

Practice

Put Makestogether, Perfect

we

Tryget creatingthe yourfollowing owntwo variables, assigning values, and experimenting with different typeslines of data. This hands-on practice will help you grasp the concept of variables in Python.code:

sum-with-variable.png