/

Basics of Working with Python for Developers

Basics of Working with Python for Developers

tags: [“Python”, “programming”, “variables”, “expressions”, “statements”, “comments”, “indentation”]

Python is a versatile programming language that is popular among developers due to its simplicity and readability. In this blog post, we will cover some fundamental concepts of working with Python.

Variables

In Python, variables are used to store data values. Creating a variable is as simple as assigning a value to a label using the = assignment operator. Here’s an example:

1
name = "Roger"

You can also assign numeric values to variables:

1
age = 8

It’s important to note that variable names can consist of letters, numbers, and underscores, but they cannot start with a number. Here are some valid examples of variable names:

1
2
3
4
5
6
name1
AGE
aGE
a11111
my_name
_name

However, variable names like 123, test!, and name% are invalid.

Additionally, it’s important to avoid using Python keywords as variable names. Keywords are reserved words in the Python programming language, such as for, if, while, import, and more. Python will notify you if you try to use a keyword as a variable.

Expressions and Statements

In Python, an expression is a piece of code that returns a value. For example:

1
2
1 + 1
"Roger"

On the other hand, a statement is an operation performed on a value. For example, assigning a value to a variable and printing its value are two different statements:

1
2
name = "Roger"
print(name)

A program in Python consists of a series of statements. Each statement is typically written on a separate line. However, you can use a semicolon to write multiple statements on a single line:

1
name = "Roger"; print(name)

Comments

In Python, comments are used to add notes or explanations to the code. Any text following a hash mark (#) is ignored and considered a comment. For example:

1
2
3
# This is a commented line

name = "Roger" # This is an inline comment

Comments are helpful for making your code more readable and for providing context to yourself and other developers who might be working with your code.

Indentation

Indentation plays a crucial role in Python as it determines the structure and hierarchy of code blocks. Unlike some other programming languages, where indentation is not significant, Python uses indentation to define blocks of code. Here’s an example:

1
2
name = "Flavio"
print(name)

If you were to run this code, you would encounter an IndentationError: unexpected indent error because the indentation is incorrect. In Python, everything indented belongs to a specific block, such as a control statement, conditional block, function, or class body.

By understanding these basics of working with Python, you will be well-equipped to start building more complex programs and exploring the various features and capabilities that Python offers.