Python syntax

Day 3: Python Syntax: Variables, Data Types, and Operations

Introduction

Congratulations on writing your first “Hello, World!” program! Now that you know how to run Python code, it’s time to dive into the core building blocks of any program: Python syntax. This article will help you master the fundamental rules of Python syntax. Today, we’ll cover the fundamental concepts of variables, data types, and basic arithmetic. These are the foundational tools you will use every single time you write a Python program, so mastering them is a crucial step in your coding journey.

You can follow along by running each code snippet in an online editor like Trinket or Replit.

Python syntax

1. Variables and Data Types: Storing Information

In Python, a variable is like a labeled container for storing information. You give it a name and assign a value to it using the = sign. Python is a dynamically typed language, which means you don’t need to specify the variable’s type; the interpreter figures it out automatically. This makes Python’s syntax simpler and more flexible for beginners compared to languages like Java or C++, where you must declare the variable type explicitly. Understanding these concepts is a core part of learning Python syntax.

Here’s how to create variables and assign them values:

Python
# Assigning values to variables
name = "John"
age = 25
height = 5.9
is_student = True

# Printing variables to the console
print(name)        # Output: John
print(age)         # Output: 25
print(height)      # Output: 5.9
print(is_student)  # Output: True
Python syntax

You can also reassign a new value to a variable at any time. The variable will then hold the new value and, if the type changes, Python will adjust automatically. For example, you could change age = 25 to age = "twenty-five", and the variable would now be a string.

The values you assign to a variable belong to different data types:

  • Strings (str): Used to store text. They are always enclosed in quotes (" " or ' '). Strings are immutable, meaning once you create a string, you cannot change individual characters within it.
  • Integers (int): Used for whole numbers, both positive and negative, like 25 or -10. Integers are ideal for counting and simple mathematical operations without decimals.
  • Floats (float): Used for numbers with a decimal point, like 5.9 or 3.14. Floats are necessary for calculations that require precision, such as scientific and financial applications.
  • Booleans (bool): Used for true or false values, either True or False. Booleans are fundamental for making logical decisions in your code, such as in conditional statements.

You can always check the type of a variable using the built-in type() function:

Python
print(type(name))         # Output: <class 'str'>
print(type(age))          # Output: <class 'int'>
print(type(height))       # Output: <class 'float'>
print(type(is_student))   # Output: <class 'bool'>

For more information on built-in functions, you can always check the official Python documentation.

2. Basic Arithmetic Operations

Python can perform all the standard mathematical calculations you would expect. The way Python handles these arithmetic operations is another key component of its syntax. These operations are essential for any program that needs to work with numbers.

Python
# Basic Arithmetic
a = 10
b = 5

sum = a + b          # Addition
difference = a - b   # Subtraction
product = a * b      # Multiplication
quotient = a / b     # Division (Note: this always results in a float)
modulus = a % b      # Modulus (remainder after division)
power = a ** b       # Exponentiation (a to the power of b)

print(f"Sum: {sum}")              # Output: Sum: 15
print(f"Difference: {difference}")   # Output: Difference: 5
print(f"Product: {product}")       # Output: Product: 50
print(f"Quotient: {quotient}")     # Output: Quotient: 2.0
print(f"Remainder: {modulus}")     # Output: Remainder: 0
print(f"Power: {power}")           # Output: Power: 100000

Notice the print(f"...") syntax. This is called an f-string (formatted string literal), a powerful feature in modern Python that allows you to embed expressions directly inside a string.

A key detail to remember is that the / operator always returns a float, even if the division results in a whole number. This is a crucial rule of Python syntax. For division that rounds down to the nearest whole number, Python provides the // operator, called “floor division.” For example, 11 // 3 would result in 3, whereas 11 / 3 would result in 3.666.... You can also use shorthand assignment operators like a += 1 (equivalent to a = a + 1) to update a variable more concisely.

Python syntax

Summary

In today’s lesson on Python syntax, you learned two of the most fundamental concepts:

  • Variables and Data Types: How to store and represent different kinds of data in your programs. This is the foundation for all data manipulation.
  • Arithmetic Operations: How to perform calculations and manipulate numerical data. These are the tools that allow your programs to perform useful work.

Mastering these basics will give you a solid foundation for more complex programming. These concepts are the first steps to building logic and functionality into your code.


The journey of learning what Python is is an exciting one, and this “30 Days of Python” series is designed to be your roadmap. Happy coding!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *