Skip to content

Commit 29ff8a5

Browse files
authored
[Edit] Python: Variables (#7436)
* [Edit] Python: Variables * updated FAQs from PAA * Update variables.md * Update variables.md ---------
1 parent ebcdd02 commit 29ff8a5

File tree

1 file changed

+122
-21
lines changed

1 file changed

+122
-21
lines changed
Lines changed: 122 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,150 @@
11
---
22
Title: 'Variables'
3-
Description: 'A variable is used to store data that will be used by the program. This data can be a number, a string, a Boolean, a list or some other data type. Every variable has a name which can consist of letters, numbers, and the underscore character . The equal sign = is used to assign a value to a variable. After the initial assignment is made, the value of a variable can be updated to new values as needed. A variable can have a short name (like x and y) or a more descriptive name (age, grade, grocerylist). Rules for Python variables: - A variable name must start with a letter or the underscore character. It cannot start with a number. - A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _). - Variable names are case-sensitive (num, Num, and NUM are three different variables).'
3+
Description: 'Variables are used to store data that can be used and manipulated throughout a program.'
44
Subjects:
55
- 'Computer Science'
66
- 'Data Science'
77
Tags:
8-
- 'Variables'
9-
- 'Integers'
108
- 'Booleans'
9+
- 'Integers'
1110
- 'Strings'
11+
- 'Variables'
1212
CatalogContent:
1313
- 'learn-python-3'
1414
- 'paths/computer-science'
15-
- 'paths/data-science'
1615
---
1716

18-
A variable is used to store data that will be used by the program. This data can be a number, a string, a Boolean, a list or some other data type. Every variable has a name which can consist of letters, numbers, and the underscore character `_`.
17+
**Python variables** help in storing data that can be used and manipulated throughout a program. Unlike some other languages, Python doesn't require explicit declaration of a variable’s [data type](https://www.codecademy.com/resources/docs/python/data-types). Whether storing a number, a [string](https://www.codecademy.com/resources/docs/python/strings) of text, or even a complex data structure like a [list](https://www.codecademy.com/resources/docs/python/lists), variables act as containers for that information, allowing code to reference and reuse it.
18+
19+
## Creating Python Variables
20+
21+
The assignment [operator](https://www.codecademy.com/resources/docs/python/operators) (`=`) is used to create a Python variable:
22+
23+
```py
24+
x = 10
25+
name = "Alice"
26+
is_active = True
27+
```
28+
29+
In this example:
30+
31+
- `x` stores an integer (`10`)
32+
- `name` stores a string (`"Alice"`)
33+
- `is_active` stores a Boolean value (`True`)
34+
35+
Python automatically infers the type of data being assigned.
36+
37+
## Modifying Python Variables
1938

20-
The equal sign `=` is used to assign a value to a variable. After the initial assignment is made, the value of a variable can be updated to new values as needed.
39+
The assignment operator (`=`) can also be used to change the value of a Python variable after its creation:
2140

22-
## Variable Names
41+
```py
42+
# Creating a variable
43+
x = 10
44+
45+
# Changing the value of the variable
46+
x = 15
47+
```
48+
49+
## Python Variable Naming Conventions
2350

24-
A variable can have a short name (like `x` and `y`) or a more descriptive name (`age`, `grade`, `grocery_list`).
51+
A Python variable can have a short name (`x`, `y`, etc.) or a more descriptive name (`age`, `grade`, `grocery_list`, etc.).
2552

26-
Rules for Python variables:
53+
Here are the rules to follow while naming Python variables:
2754

28-
- A variable name must start with a letter or the underscore character. It cannot start with a number.
29-
- A variable name can only contain alpha-numeric characters and underscores (`A`-`z`, `0`-`9`, and `_`).
55+
- A variable name should begin with a letter or the underscore character. It cannot start with a number.
56+
- A variable name can only include alpha-numeric characters and underscores (`A`-`z`, `0`-`9`, and `_`).
3057
- Variable names are case-sensitive (`num`, `Num`, and `NUM` are three different variables).
3158

32-
## Examples
59+
These are some valid variable names:
60+
61+
```py
62+
my_var = 1
63+
_myvar = 2
64+
myVar3 = 3
65+
```
3366

34-
These are all valid variable names and assignment:
67+
These are some invalid variable names:
3568

3669
```py
37-
user_name = "@sonny420"
38-
user_id = 100
39-
verified = False
70+
3var = 10 # Starts with a number (invalid)
71+
my-var = 20 # Contains a hyphen (invalid)
72+
if = "hello" # 'if' is a reserved keyword (invalid)
4073
```
4174

42-
A variable's value can be changed after assignment
75+
## Example 1: Basic Arithmetic with Python Variables
76+
77+
This example adds two Python variables:
78+
79+
```py
80+
a = 5
81+
b = 3
82+
83+
sum_result = a + b
84+
85+
print("Sum:", sum_result)
86+
```
4387

44-
```codebyte/py
45-
points = 100
46-
points = 120
88+
The output generated by this code is:
4789

48-
print(points)
90+
```shell
91+
Sum: 8
4992
```
93+
94+
## Example 2: Swapping Python Variables
95+
96+
Python makes it easy to swap values between two variables:
97+
98+
```py
99+
x = 10
100+
y = 20
101+
102+
x, y = y, x
103+
104+
print("x =", x)
105+
print("y =", y)
106+
```
107+
108+
The output generated by this code is:
109+
110+
```shell
111+
x = 20
112+
y = 10
113+
```
114+
115+
## Codebyte Example: Using Python Variables in Strings
116+
117+
Python variables can be used within strings using f-strings (Python 3.6+):
118+
119+
```codebyte/python
120+
name = "Alice"
121+
122+
age = 30
123+
124+
print(f"My name is {name} and I am {age} years old.")
125+
```
126+
127+
## Frequently Asked Questions
128+
129+
### 1. What are the four types of variables in Python?
130+
131+
Python defines four main types of variables based on their scope: local, global, instance and class. Here's how they work:
132+
133+
- **Local variables**: Declared inside a function and accessible only within that function.
134+
- **Global variables**: Declared outside all functions and accessible throughout the program.
135+
- **Instance variables**: Belong to a specific object created from a class.
136+
- **Class variables**: Shared across all instances of a class.
137+
138+
### 2. How to name Python variables?
139+
140+
When naming variables in Python follow these rules:
141+
142+
- Begin with a letter or underscore (`_`) and never with a number.
143+
- Use only letters, numbers, and underscores (no spaces or special characters).
144+
- Variable names are case-sensitive (`score`, `Score`, and `SCORE` are different).
145+
- Avoid using Python reserved keywords like `if`, `while`, or `class`.
146+
- Follow PEP 8 style guidelines and use `lowercase_with_underscores` for readability.
147+
148+
### 3. How do you declare variable types in Python?
149+
150+
Python uses dynamic typing, so you don’t have to declare types explicitly, the type is inferred when you assign a value.

0 commit comments

Comments
 (0)