Introduction
Python is a versatile programming language known for its simplicity and readability. In this guide, we'll explore the fundamental concepts of Python syntax, including variables, data types, and operators.
Variables
Variables are used to store data in Python. They are created by assigning a value to a name. Python is dynamically typed, meaning you don't need to declare the data type of a variable explicitly. Here's how you create variables:
# Integer variable
age = 30
# String variable
name = "John Doe"
# Float variable
salary = 50000.50
Data Types
Python supports several data types:
- int: Integer
- float: Floating-point number
- str: String
- bool: Boolean (True or False)
- list: Ordered collection of items
- tuple: Immutable ordered collection of items
- dict: Key-value pairs
You can use the type()
function to determine the data type of a variable.
age = 30
print(type(age)) # Output: <class 'int'>
name = "John Doe"
print(type(name)) # Output: <class 'str'>
Operators
Python provides a variety of operators for performing operations on variables and values. Common operators include:
- Arithmetic Operators: +, -, *, /, %, ** (exponentiation)
- Comparison Operators: ==, !=, <, >, <=, >=
- Logical Operators: and, or, not
Here's an example using arithmetic and comparison operators:
x = 10
y = 5
sum = x + y
is_equal = x == y
print("Sum:", sum) # Output: Sum: 15
print("Is equal:", is_equal) # Output: Is equal: False
Conclusion
Understanding Python syntax basics is crucial for writing Python programs. Variables, data types, and operators are the building blocks of Python, and mastering them will enable you to create more complex applications.