About the basic syntax of Python
This is an article about the basic syntax of Python.
Python is a programming language famous for its concise and easy-to-read syntax.
Let’s find out through an example together.
Variables and data types
There is no need to specify a separate type when declaring a variable in Python.
For example, you can declare a variable like this:
name = "Alice"
age = 25
height = 165.5
In the above example, name is a string, age is an integer type, and height is a real number variable.
Conditional statement (if statement)
Conditional statements in Python can be written using the if, elif, and else keywords.
For example, you can write a conditional statement like this:
x = 10
if x > 0:
print("It's a positive number.")
elif x < 0:
print("It's a negative number.")
else:
print("It's 0.")
In the above example, it determines whether variable x is positive, negative, or 0 and outputs the result.
Loop statement (for statement)
Python’s for statement is used to iterate through sequence data types.
For example, you can traverse the list and output elements as follows:
fruits = ["apple", "banana", "strawberry"]
for fruit in fruits:
print(fruit)
In the above example, the elements of the list called fruits are iterated one by one and output.
function
When defining functions in Python, use the def keyword.
For example, you could define a greeting function like this:
def say_hello(name):
print("Hello, " + name + "!")
say_hello("Alice")
In the example above, a function called say_hello is defined and the function is called to output “Hello, Alice!”
Conclusion
We briefly looked at the basic syntax and examples of Python.
Python has a concise and readable syntax and is used in a variety of programming fields.
Next time, we will meet you on more diverse topics. thank you