Sitemap

Python Basic : Syntax and Variables

2 min readJul 22, 2024

--

Python is known for its simplicity and readability, making it an excellent choice for beginners. In this post, we will explore the basic syntax of Python, understand data types, and learn how to work with variables. Let’s get started!

Press enter or click to view image in full size
Photo by Creatopy on Unsplash
  1. Basic Python Syntax

Python uses indentation to define blocks of code, making the code clean and readable. Here are some fundamental syntax rules:

Comments : Use # for single-line comments.

# This is a comment

Using Triple Quotes (''' or """) for multi-line comments.

"""
This is a multiline comment
using triple double quotes.
It can span multiple lines.
"""

Print statement : Use the print() function to output text.

print("Hello, world!")

Indentation : Indentation is crucial in Python. It defines the scope of loops, functions, and conditionals.

if True:
print("This is indented")

Variables : Variables do not require explicit declaration to reserve memory space. The assignment happens automatically when you assign a value to a variable.

x = 5
y = "Hello"

2. Data types and Variables

Get codestories’s stories in your inbox

Join Medium for free to get updates from this writer.

Python supports various data types. Here are some of the most commonly used:

Numbers: Integers, floats, and complex numbers.

integer_var = 10
float_var = 10.5
complex_var = 1 + 2j

Strings: A sequence of characters enclosed in quotes.

string_var = "Hello, Python!"

Lists: Ordered and mutable collection of items.

list_var = [1, 2, 3, "Python"]

Tuples: Ordered and immutable collection of items.

tuple_var = (1, 2, 3, "Python")

Dictionaries: Unordered collection of key-value pairs.

dict_var = {"name": "Anagha", "age": 25}

Booleans: Represent True or False.

bool_var = True

None : This variable represents nothing id assigned in this variable.

none_var = None

--

--

codestories
codestories

Written by codestories

Somewhere between humans and AI, trying to understand both. Writing what I learn along the way.