Variables and Data Types

What are Variables and Data Types?

In Python, a variable is a named storage location used to store data values. Think of it like a box that holds a value that can be accessed later in the program by referring to the variable name.

There are several data types in Python, and each variable in Python has a data type associated with it. These are some of the most commonly used data types:

•  Strings: Strings are used to store text in Python. They are enclosed in quotes, either single quotes or double quotes. For example: "Hello, World!" or 'Python is awesome!'
•  Integers: Integers are used to store whole numbers (positive, negative or zero) in Python. For example: 10, 20, -30, 0.
  Floats: Floats are used to store decimal numbers in Python. For example: 3.14, -1.5, 0.0.
•  Booleans: Booleans are used to store True or False values. For example: True, False.
Dictionaries: These are collections of key-value pairs. For example, a dictionary that maps names to ages might look like this: {"Alice": 27, "Bob": 34, "Charlie": 19}.
•  Lists: These are ordered collections of values. For example, a list of integers might look like this: [1, 2, 3, 4, 5].
•  Tuples: These are similar to lists but are immutable, meaning their values cannot be changed once defined.

In the code above, I assigned the string "Alice" to the variable ‘name’, the integer ‘30’ to the variable ‘age’, the float ‘3.14’ to the variable ‘pi’, and the boolean ‘True’ to the variable ‘is_student’. 

Once you have assigned a value to a variable, you can use that variable throughout your program to refer to that value. For example, if you wanted to print the value of the name variable, you could use the ‘print()’ function like this: print(name) This would output: Alice. 

These are just a few examples of the data types in Python. As you learn more about the language, you'll encounter other data types like lists, tuples, and dictionaries. 

In Python, you don't have to tell it what type of data you're gonna put into a variable. The data is dynamically typed, which means it figures out what type of data you're using based on what you put in the variable. You can assign a number, a string, or whatever, and Python will do the rest. It's super convenient and saves a lot of time and effort.

If you would like more information or examples over variables and data types, refer to the link below: 

https://docs.python.org/3/library/stdtypes.html

In the example above, I created variables of different data types such as an integer, a floating-point number, a string, and a boolean value. I then print the values of these variables to the console using the print() function. Next, I used the variables in expressions, such as adding the integer and floating-point values together and assigning the result to a new variable called result. Finally, I used the upper() method on the my_string variable to convert it to uppercase and assign the result to a new variable called upper_string, which I then printed to the console.

Sources

The Python documentation on variable​s and types: https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator

The Python documentation on strings: https://docs.python.org/3/library/stdtypes.html#string-methods

W3Schools Python tutorial on variables and data types: https://www.w3schools.com/python/python_variables.asp

GeeksforGeeks Python tutorial on variables and data types: https://www.geeksforgeeks.org/variables-and-data-types-in-python/