Getting Started with Python: Understanding Syntax, Variables, and Data Types.
Welcome to the first article in the Python learning series. Python is one of the most popular programming languages due to its simplicity and versatility. Whether you’re interested in data science, web development, or automation, Python has something for everyone.
In this article, I’ll cover the basic building blocks of Python, including syntax, variables, and data types. By the end, you will have a solid foundation for syntax, variables, and data types in Python.
- Introduction to Python Syntax
- Variables in Python
- Python Data Types (Primitive and Compound)
1. Introduction to Python Syntax
Python’s syntax is straightforward and highly readable, making it a great first programming language. Let’s explore the basics.
Indentation and Code Blocks
In Python, indentation is critical. It defines how statements are grouped, instead of curly braces or keywords in other languages. Python relies on whitespace (spaces or tabs) to group statements.
if condition:
# indented block of code
print("Condition is True")
If the indentation is not consistent, Python will raise an IndentationError.
if condition:
print("Condition is True") # ERROR : IndentationError: expected an indented block after 'if' statement
Comments
Comments start with the # symbol. They are useful for adding explanations to your code. Python ignores comments during execution.
# This is a comment
print("Hello, world!") # This comment is inline with the code
2. Variables in Python
A variable in Python is a name given to a value stored in memory. You can assign values to variables without specifying the data type, as Python infers the type based on the value assigned. We must follow the rules of Python variable naming rules. It is called the Python naming convention.
- It must start with a letter or underscore (_).
- Variable can contain letters, digits, and underscores but cannot start with a digit.
- Python is case-sensitive. As an example name and Name are different variables.
name = "Alice" # String
age = 30 # Integer
is_active = True # Boolean
3. Python Data Types (Primitive and Compound)
Python supports several data types, these define the kinds of values a variable can hold. Data types can be categorized into primitive and compound data types.
Primitive Data Types
Integer
Stores whole numbers (positive or negative)
x = 10
y = -5
Float
Stores decimal numbers
a = 2.345
String
A sequence of characters enclosed in quotes (single or double).
message = “Welcome, Python!”
Boolean
Represents two values — True or False
is_active = True
is_empty = False
Next, let’s move to compound data types 😊👇
Compound Data Types
(i). Lists
Lists are ordered collections of items. Lists can contain elements of different data types, and those are mutable, meaning you can change, add, or remove items after the list has been created. Lists can contain duplicate elements. The List is defined using square brackets [].
Lists are useful when you need an ordered collection of items that can change over time.
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mix_list = [1, 2, 3, "apple", True]
print(fruits[0]) # Outputs: apple
fruits[1] = "blueberry" # Change fruits list. -> Replaces 'banana' with 'blueberry'
fruits.append("orange") # Add item to list.
fruits.remove("cherry") # Remove 'cherry' from list.
(ii). Tuple
A tuple is similar to a list but immutable. Once created, you cannot modify it. Keep items in order(similar to the List). Lists can contain duplicate elements. Tuples are defined using parentheses (). Items of Tuple can still be accessed by index.
Tuples are useful when you want to ensure that the data remains unchanged throughout your program.
coordinates = (10,20,30)
print(coordinates[0]) # Outputs: 10
(iii). Dictionary
A dictionary is a collection of key-value pairs. Each Key must be unique and No duplicate keys, but values can be duplicated. The dictionary is Mutable and maintains the insertion order of elements (from Python 3.7+). Dictionaries are defined using curly braces {}.
Dictionaries are highly useful when you need to map relationships between keys and values, such as storing information about a student or managing configuration settings.
student = {"name": "Alice", "age": 25, "is_enrolled": True}
print(student["name"]) # Outputs: Alice
student["age"] = 26 # Updates age to 26
student["grade"] = "A" # Add a new key 'grade'
del student["age"] #Removing key-value pairs related to 'age' key.
(iv). Set
A set is an unordered collection of unique items. Sets are defined using curly braces {}, and they automatically remove duplicates. Sets are mutable.
Sets are ideal when you need to manage unique items and perform mathematical set operations.
my_set = {1, 2, 3, 4, 5}
my_set.add(6) # Adds the value 6
my_set.remove(3) # Removes the value 3
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2)) # Outputs: {1, 2, 3, 4, 5}
print(set1.intersection(set2)) # Outputs: {3}
unique_numbers = {1, 2, 3, 3, 4}
print(unique_numbers) # automatically remove duplicates | Outputs: {1, 2, 3, 4}
Congratulations on completing your first lesson in Python! Now that you have leveled up with the Python basics such as syntax, variables, and data types, you can start writing simple programs. You can explore more advanced topics such as control structures, loops, and functions in upcoming articles.
👉 Control Structures in Python — Conditional Statements (if) and Loops
Github Repo: https://github.com/Nuwan-Abeywickrama/python-learn/tree/master
You can follow me for upcoming articles. 🔔
The comment section is open for you to share special things, and new updates, and mention mistakes regarding this content. I’m also learning new things from your comments.
I think sharing knowledge, learning new things, and helping others will help to develop a better world. Let’s share our knowledge to learn for everyone.🏆🏆🥇
Thank You. 🤝🤝