Basics of Python 01: Python Syntax

25 Oct 2023 By Code Bricks

Basics of Python

Python is a versatile, high-level programming language known for its simplicity and readability. In this chapter, we’ll explore the foundational concepts to get you started with Python.

Python Syntax

Python uses indentation to define blocks of code. This means that code written inside loops, if statements, and functions is indented to the right. Unlike many other languages, Python does not use curly braces {} to define code blocks.

Example:

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

Variables and Data Types

In Python, you don’t need to declare the data type of a variable explicitly. The language automatically determines the data type based on the value assigned to the variable.

Here are some common data types:

  • Integers (int): Whole numbers, e.g., 5, -3.
  • Floating Point (float): Numbers with a decimal point, e.g., 5.0, -3.5.
  • Strings (str): Sequence of characters, e.g., "Hello, World!".
  • Booleans (bool): True or False values.

Basic Operators

Python includes a variety of operators for performing basic arithmetic and logical operations.

  • Arithmetic Operators: +, -, *, /, //, %, **.
  • Comparison Operators: ==, !=, <, >, <=, >=.

User Input and Output

To receive input from the user, Python provides the input() function. For output, we use the print() function.

Example of getting user input:

name = input("Enter your name: ")
print(f"Hello, {name}!")

Comments in Python

Comments are lines in the code that Python ignores. They’re used to explain the code or add notes for developers. In Python, you can create a comment by using the # symbol.

Example:

# This is a comment
print("This is not a comment")

By understanding these basics, you’ve taken your first steps into the world of Python programming. As you continue your journey, you’ll find that Python’s simplicity and vast ecosystem make it a great choice for a variety of applications, from web development to data analysis. Stay tuned for our next chapters where we delve deeper into Python’s features!