In this post, we will explore the fundamental syntax of Python. Understanding these basics will help you build a strong foundation as you progress through more advanced topics.
Variables and Data Types
1. Variables
Variables are used to store data in a program. You can think of them as containers that hold information. In Python, you don’t need to declare the type of a variable explicitly. You simply assign a value to it.
Example:
python
# Assigning values to variables
name = “Alice”
age = 25
is_student = True
2. Data Types
Python has several built-in data types. Here are the most commonly used ones:
- Integers (int): Whole numbers without a decimal point.
python
age = 25
- Floats (float): Numbers with a decimal point.
python
temperature = 36.6
- Strings (str): Sequence of characters enclosed in quotes.
python
name = “Alice”
- Booleans (bool): Represents True or False.
python
is_student = True
3. Type Casting
You can convert a value from one type to another using type casting.
Example:
python
# Integer to float
x = 5
y = float(x) # y will be 5.0
# Float to integer
z = int(y) # z will be 5
# String to integer
number_str = “123”
number_int = int(number_str) # number_int will be 123
Operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
Operator | Description | Example |
+ | Addition | x + y |
– | Subtraction | x – y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus | x % y |
** | Exponentiation | x ** y |
// | Floor Division | x // y |
Example:
python
a = 10
b = 3
print(a + b) # Output: 13
print(a – b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a % b) # Output: 1
print(a ** b) # Output: 1000
print(a // b) # Output: 3
2. Comparison Operators
Comparison operators are used to compare two values.
Operator | Description | Example |
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Example:
python
x = 5
y = 3
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: True
print(x < y) # Output: False
print(x >= y) # Output: True
print(x <= y) # Output: False
3. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example |
and | Returns True if both statements are true | x and y |
or | Returns True if one of the statements is true | x or y |
not | Reverses the result, returns False if the result is true | not x |
Example:
python
x = True
y = False
print(x and y) # Output: False
print(x or y) # Output: True
print(not x) # Output: False
Input and Output
1. Taking User Input
You can use the input() function to take input from the user. The input is always returned as a string.
Example:
python
name = input(“Enter your name: “)
print(“Hello, ” + name)
2. Printing Output
You can use the print() function to display output to the console.
Example:
python
# Print multiple values
print(“Name:”, name, “Age:”, age)
# Using f-strings for formatted output (Python 3.6+)
print(f”Hello, {name}. You are {age} years old.”)
Conclusion
In this post, we covered the basics of Python syntax, including variables, data types, operators, and basic input/output. Understanding these fundamentals is crucial for progressing to more complex topics. In the next post, we will dive into control structures, including conditional statements and loops. Stay tuned!
1 Comment