Introduction
Welcome to PythonSage!, your ultimate guide to everything Python! I'm
Abdullah, and today, we will explore the basics of comments and variables in
Python. Whether you are just starting out or want to refresh your knowledge,
this guide will give you easy-to-understand explanations and practical
examples to help you grasp these important concepts in Python
programming.
Comments in python
Comments are parts of the code that the interpreter ignores. They are used
to explain and describe the code, making it easier for others (or even
yourself) to understand what the code does. In Python, there are two types
of comments: single-line and multi-line.
Single-Line Comments
Single-line comments start with the hash character (#
) and extend to the end of the line.
Here's an example:
# This is a single-line comment
print("Hello, World!")
Multi-Line Comments
Multi-line comments can be created in Python using triple quotes (`“””` or
`'''`). This type of comment allows the coder to write the comment in
multiple lines.
Here's an example:
"""
This is a multi-line comment.
It spans multiple lines.
"""
print("Python Sage")
Variables in Python
If talking in general variables are used to store the data that can be
referenced and changed throughout the code or, maybe, changed. Luckily in
Python, you don’t need to declare the data type of the python, python
automatically infers its type when you assign the value to a variable.
Create variables
In Python, we can simply create the variable by assigning it a value using
the equal sign (=):
Example:
x = 5
y = "Hello"
If we discuss the example in “x” we store the integer value which is 5 in, python automatically inferred its data type as Integer, and in “y” we
store the characters in the double quote which are “Hello” and that
basically a string so python automatically inferred its type as String.
Below are Some Variables Naming Rules
- When naming variables in Python, follow these rules:
- Variable names must start with a letter or an underscore (_).
- The rest of the name can contain letters, numbers, or underscores.
- Variable names are case-sensitive.
Examples:
Examples of valid variable names:
my_variable = 10
_variable = 20
var1 = 30
Examples of invalid variable names:
1variable = 10 # Starts with a number
my-variable = 20 # Contains a hyphen
Reassigning Variables
We can also reassign value to a variable of different data types.
Example:
x = 5
x = "Hello" # x is now a string
Practical Examples
Below are some practical examples to understand better:
Example 1:
Using comments to explain the code:
# Define a variable to store the user's name
user_name = "Python Sage Audience"
# Print a greeting message
print("Hello, " + user_name + "!")
Example 2:
Working with a different type of variable types:
# Integer variable
age = 25
# Float variable
height = 5.9
# String variable
name = "abdullah"
# Boolean variable
is_student = True
# Print the variables
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is a student:", is_student)
Here are the Exercises with the Solution PDF File.
Conclusion:
Comments and variables are essential foundations of Python programming.
Comments clarify code, enhancing its readability, while variables store and
manage data. Mastering these fundamentals is key to becoming proficient in
Python. Keep practicing and experimenting with various comment styles and
variable uses to strengthen your skills.
Stay connected with PythonSage for further tutorials and guides on Python
programming!
Happy Coding!