Python 3 – Basic Syntax
Python is an easy-to-learn programming language that is widely used by developers for different applications. It is known for its simplicity, flexibility, and readability. Python syntax is also straightforward and easy to understand, which makes it a perfect choice for beginners.
This article will provide you with an introduction to Python 3 basic syntax.
Comments
In Python, comments are lines of code that are ignored by the interpreter. They are used to explain the code and provide additional information to other programmers.
To add a comment in Python, use the hash sign (#) before the text:
# This is a comment
print("Hello, World!")
Variables
In Python, variables are used to store values. You can assign different data types to a variable, such as numbers, strings, and lists.
To assign a value to a variable, use the equal sign (=):
a = 10
b = "hello"
c = [1, 2, 3]
You can also assign multiple values to multiple variables in a single line:
x, y, z = 1, 2, 3
Naming conventions
When naming variables in Python, use lowercase words separated by underscores. Avoid using reserved keywords, such as “if” and “while.”
my_variable = 5
Data Types
Python has several built-in data types, including:
- Numbers (int, float, complex)
- Strings
- Booleans
- Lists
- Tuples
- Sets
- Dictionaries
Numbers
In Python, you can use numbers in different ways, such as performing mathematical operations or storing values.
a = 10 # int
b = 3.14 # float
c = 2 + 3j # complex
Strings
Strings are used to store text in Python. You can use single quotes, double quotes, or triple quotes to represent strings.
my_string = "Hello, World!"
Booleans
Booleans are used to represent true or false values.
a = True
b = False
Lists
Lists are used to store collections of items in Python. Each item in a list is separated by a comma and enclosed in square brackets.
my_list = [1, 2, 3, "hello"]
Tuples
Tuples are similar to lists, but they are immutable, meaning that their values cannot be changed once they are created.
my_tuple = (1, 2, 3, "hello")
Sets
Sets are used to store unique values in Python.
my_set = {1, 2, 3, 3, 3}
Dictionaries
Dictionaries are used to store key-value pairs in Python. Each key-value pair is separated by a colon, and each pair is separated by a comma.
my_dict = {"name": "John", "age": 30}
Operators
Operators are used to perform different types of operations in Python, such as mathematical or logical operations.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations, such as addition or subtraction.
x = 10
y = 5
print(x + y) # addition
print(x - y) # subtraction
print(x * y) # multiplication
print(x / y) # division
print(x % y) # modulus
print(x ** y) # exponentiation
Comparison Operators
Comparison operators are used to compare two values and return a boolean value.
x = 10
y = 5
print(x > y) # greater than
print(x < y) # less than
print(x == y) # equal to
print(x != y) # not equal to
print(x >= y) # greater than or equal to
print(x <= y) # less than or equal to
Logical Operators
Logical operators are used to combine multiple conditions and return a boolean value.
x = 10
y = 5
print(x > 5 and y < 10) # and operator
print(x > 5 or y > 10) # or operator
print(not(x > 5 and y < 10)) # not operator
Control Flow Statements
Control flow statements are used to control the flow of a program, such as loops or conditionals.
If-else Statements
If-else statements are used to execute code based on a condition.
x = 10
y = 5
if x > y:
print("x is greater than y")
else:
print("x is less than or equal to y")
Loops
Loops are used to execute a block of code multiple times.
For Loops
For loops are used to iterate over a sequence of values.
my_list = [1, 2, 3, 4, 5]
for x in my_list:
print(x)
While Loops
While loops are used to execute a block of code as long as a condition is true.
i = 0
while i < 5:
print(i)
i += 1
Functions
Functions are used to encapsulate a block of code that can be reused multiple times.
def my_function(name):
print("Hello, " + name)
my_function("John")
Conclusion
That’s it for this introduction to Python 3 basic syntax. We covered comments, variables, data types, operators, control flow statements, and functions.
It is essential to understand the basics to write effective code in Python. Once you are comfortable with these concepts, you can start exploring more advanced topics in Python.