@@ -14,7 +14,7 @@ Python is a high-level, interpreted programming language known for its readabili
14
14
15
15
In Python, declaring a variable is as simple as assigning a value to a name using the = operator:
16
16
17
- ``` Python
17
+ ``` python
18
18
# Examples of variables
19
19
name = " Alice" # string variable
20
20
age = 25 # integer variable
@@ -31,15 +31,17 @@ is_student = True # boolean variable
31
31
- Variable names are case-sensitive (age and Age would be two different variables).
32
32
33
33
1 . Examples of valid variable names:
34
- ``` Python
34
+
35
+
36
+ ``` python
35
37
first_name = " Alice"
36
38
_age = 30
37
39
height_in_cm = 175
38
-
39
40
```
40
41
41
42
2 . Examples of invalid variable names:
42
- ``` Python
43
+
44
+ ``` python
43
45
2nd_variable = " invalid" # Starts with a number
44
46
first- name = " invalid" # Contains a hyphen
45
47
```
@@ -54,7 +56,7 @@ first-name = "invalid" # Contains a hyphen
54
56
- Tuples (tuple): Ordered, immutable collection of items (e.g., (1, 2, 3))
55
57
- Dictionaries (dict): Key-value pairs (e.g., {"name": "Alice", "age": 25})
56
58
57
- ``` Python
59
+ ``` python
58
60
x = 42
59
61
print (type (x)) # Output: <class 'int'>
60
62
@@ -67,7 +69,7 @@ print(type(z)) # Output: <class 'str'>
67
69
68
70
### Reassigning Variables
69
71
70
- ``` Python
72
+ ``` python
71
73
number = 10
72
74
print (number) # Output: 10
73
75
@@ -76,21 +78,20 @@ print(number) # Output: 15
76
78
77
79
number = " ten" # Reassign with a string value
78
80
print (number) # Output: ten
79
-
80
81
```
81
82
82
83
### Constants
83
84
84
85
Although Python does not have a built-in way to declare constants (variables that should not change), it’s a common convention to use all-uppercase names for variables intended to be constant:
85
86
86
- ``` Python
87
+ ``` python
87
88
PI = 3.14159
88
89
GRAVITY = 9.8
89
90
```
90
91
91
92
### Multiple Variable Assignment
92
93
93
- ``` Python
94
+ ``` python
94
95
a, b, c = 1 , 2 , 3
95
96
print (a) # Output: 1
96
97
print (b) # Output: 2
@@ -104,7 +105,7 @@ print(x, y, z) # Output: 100 100 100
104
105
- Global Variables: Defined outside of a function and can be accessed anywhere in the code.
105
106
- Local Variables: Defined inside a function and can only be accessed within that function.
106
107
107
- ``` Python
108
+ ``` python
108
109
global_var = " I am global"
109
110
110
111
def my_function ():
0 commit comments