Python Fundamentals: Data Types and Variables

Welcome to the first post in our series - Python. In this post, we will cover the basics of Python, including data types and variables.
Python is a high-level programming language known for its simplicity and readability. One of the key features of Python is its use of indentation to indicate code blocks rather than curly braces or keywords. This makes Python code easy to read and understand, making it an excellent language for beginners.

Data Types in Python

In Python, we can use several built-in data types to store and manipulate data. The most common data types are integers, floating-point numbers, and strings.

Integers are whole numbers, such as 1, 2, or 3, floating-point numbers are numbers with decimal points, such as 3.14, and strings are sequences of characters, such as "coding" or "ambitions".

To store these data types, we will create variables; for example, to create an integer variable named "x" and assign it the value 3, we would use the following code:

x = 3

To create a floating-point variable named "y" with an assigned value of 3.14, we would write:

y = 3.14

And to create a sting variable named "z" with the value "coding", we would use the following:

z = "coding" (Notice the text string is wrapped in double quotation marks.)

We can also use built-in functions of Python, such as 'type()' to check the data of a variable. By using print(type(#)) with "x", "y", and "z" in place of the "#" respectfully, you will see the following printed: 'int', 'float', and 'str', representing integer, floating-point, and string.

Variable Naming Conventions

In Python, variable names can contain letters, numbers, and underscores. They must always begin with a letter or an underscore and cannot be a keyword that exists in the Python language.

For example, the following are valid variable names:

my_variable

x1

_private

While the following are not valid variable names:

1x (starts with a number)

my-variable (contains a dash)

class (a keyword in Python)

Conclusion

In this post, we've covered the basics of data types and variables in Python. We've seen how to create variables to store different data types and how to check the data type of a variable using the 'type()' function. We've also discussed variable naming conventions in Python. In the next post, we will cover Python's control structures, such as loops and conditionals, and how to use them to control the flow of our code.

Note: One of my favorite aspects of coding is being able to name variables - this allows you to bring meaning to your code to ease the programming process.

Comments