Introduction to Variables in Python
-----------------------------------
- A variable is a name given to a value in Python.
- To assign a value to a variable, use the equals sign (=).
- Variables in Python are dynamically typed, meaning you can assign
a variable of one type and then reassign it to another type.
Example:
x = 10 # x is an integer
x = "Hello" # x is now a string
Assigning and Reassigning Variables
-----------------------------------
- Assigning a value to a variable is also known as initializing the
variable.
- Reassigning a variable is simply changing the value associated with
the variable name.
Example:
y=5
y = y + 2 # y is now 7
Swapping Values of Two Variables
--------------------------------
- Swapping the values of two variables is a common operation in
programming.
- In Python, you can swap the values of two variables using a
temporary variable.
, Example:
a = 10
b = 20
temp = a
a=b
b = temp
Understanding Temporary Variables
---------------------------------
- A temporary variable is a variable that is used to store a value for a
short period of time.
- In the previous example, the variable 'temp' is a temporary variable
used to store the initial value of 'a' while 'a' is assigned the value of
'b'.
- Temporary variables can make your code easier to read and
understand by making it clear what values are being swapped.
Basic Syntax of Python Programming
----------------------------------
- Python uses indentation to indicate blocks of code.
- The basic syntax of a Python program is:
<statement 1>
<statement 2>
...
<statement N>
- Use the '#' character to add comments to your code.
- To print something to the console, use print(<value>).
Example:
x=5
y = 10
# this is a comment
print(x + y)