Python Logo

Syntax


Python syntax is the set of rules that define how Python code should be written. It includes things like the basic building blocks of Python code (variables, functions, loops, etc.), how to use operators, and how to format your code.

Here is a brief overview of Python syntax, with some examples:

Variables

Variables are used to store data in Python. They are declared by assigning them a value, using the = operator. For example:

 

Python

my_variable = 5
 

Once a variable has been declared, you can access its value by using its name. For example:

 

Python

print(my_variable)
 

This will print the value 5 to the console.

Functions

Functions are used to group together code that performs a specific task. They are defined using the def keyword, followed by the function name and a list of parameters. For example:

 

Python

def greet_user(name):
  print(f"Hello, {name}!")
 

This function defines a function called greet_user(), which takes one parameter, name. The function prints a greeting to the console, using the name parameter.

To call a function, you simply use its name, followed by parentheses and any arguments that you want to pass to it. For example:

 

Python

greet_user("Bard")
 

This will print the greeting "Hello, Bard!" to the console.

Loops

Loops are used to repeat a block of code until a certain condition is met. There are two main types of loops in Python: for loops and while loops.

for loops are used to iterate over a sequence of items. For example:

 

Python

my_list = [1, 2, 3, 4, 5]

for item in my_list:
  print(item)
 

This will print the numbers 1, 2, 3, 4, and 5 to the console, one per line.

while loops are used to repeat a block of code until a condition is met. For example:

 

Python

i = 0

while i < 5:
  print(i)
  i += 1
 

This will print the numbers 0, 1, 2, 3, and 4 to the console, one per line.

Operators

Operators are used to perform operations on data. Python has a wide variety of operators, including arithmetic operators (e.g., +, -, *, /), relational operators (e.g., >, <, ==, !=), and logical operators (e.g., and, or, not).

For example, the following code uses the + operator to add two numbers together:

 

Python

a = 1
b = 2

sum = a + b

print(sum)
 

This will print the number 3 to the console.

Formatting

Python code is formatted using indentation. Indentation is used to group together blocks of code. For example:

 

Python

if 5 > 2:
  print("Five is greater than two!")
 

In this example, the print() statement is indented, which indicates that it is part of the if statement. If the print() statement was not indented, Python would interpret it as a separate statement.

Python also uses whitespace to separate statements and expressions. For example:

 

Python

print("Hello, World!")
 

In this example, the whitespace between the print() statement and the string "Hello, World!" is used to separate the two statements.

Here are some additional tips for writing Python code:

  • Use descriptive variable names.
  • Add comments to your code to explain what it is doing.
  • Format your code neatly, using indentation and whitespace.
  • Test your code thoroughly to make sure that it is working correctly.

For more information on Python syntax, please see the official Python documentation: https://docs.python.org/3/reference/index.html