You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/development/python/python-variables/index.md
+43-45
Original file line number
Diff line number
Diff line change
@@ -1,61 +1,55 @@
1
1
---
2
2
slug: python-variables
3
-
author:
4
-
name: Linode Community
5
-
email: docs@linode.com
6
3
description: "Variables form the backbone of programming. So how do they work and how do you use them effectively with Python? Find out in this guide covering Python variables from assignment through scope and type."
Variables are what make programs capable of meaningful action and complexity. So understandably you want to know how they work for any language you are working with.
20
+
Variables are what make programs capable of meaningful action and complexity. Understandably, you want to know how they work for any language you are working with.
27
21
28
-
Python variables are straightforward once you see their principles in action. This guide shows you the ins and outs of Python variables, with everything from variable assignment to variable operations and scope.
22
+
Python variables are straightforward once you see their principles in action. This guide shows you the basics of Python variables, with everything from variable assignment to variable operations and scope.
29
23
30
24
## Assigning Variables
31
25
32
26
Python does not require variables to be declared ahead of time. Instead, you just need to assign a value to create a variable. You can do this simply with the `=` operator as shown in the example below:
33
27
34
-
```python
28
+
```command
35
29
example_variable = "This is an example string variable."
36
30
```
37
31
38
32
Python automatically determines the type of the variable. In Python, you can also reassign a variable at any time, even to a different type. For instance, the following example reassigns the `example_variable` as an integer:
39
33
40
-
```python
34
+
```command
41
35
example_variable = 5
42
36
```
43
37
44
38
You can also assign multiple variables simultaneously using commas to separate the variables and their respective values, like so:
@@ -77,18 +71,24 @@ Python variables support three popular naming conventions:
77
71
78
72
- Camel case, where the first letter is lowercase and each subsequent word in a name begins with an uppercase letter.
79
73
80
-
exampleVariable = "This is a string variable."
81
-
anotherExampleVariable = 31
74
+
```command
75
+
exampleVariable = "This is a string variable."
76
+
anotherExampleVariable = 31
77
+
```
82
78
83
79
- Snake case, where separate words are separated by underscores.
84
80
85
-
example_variable = "This is a string variable."
86
-
another_example_variable = 31
81
+
```command
82
+
example_variable = "This is a string variable."
83
+
another_example_variable = 31
84
+
```
87
85
88
86
- Pascal case, where each word in a variable name begins with an uppercase letter.
89
87
90
-
ExampleVariable = "This is a string variable."
91
-
AnotherExampleVariable = 31
88
+
```command
89
+
ExampleVariable = "This is a string variable."
90
+
AnotherExampleVariable = 31
91
+
```
92
92
93
93
Python does not have an official naming convention, so you are free to choose between these.
94
94
@@ -102,31 +102,31 @@ However, if you are looking to align with more of the Python community, you may
102
102
103
103
Variables, once assigned, can be used almost anywhere within your Python code. For instance, the `print`functiontakes a string as an argument. As the example below shows, a variable containing a string can be substituted in place of a plain string.
104
104
105
-
```python
105
+
```command
106
106
example_variable = "World"
107
107
108
108
print(example_variable)
109
109
```
110
110
111
-
{{< output >}}
111
+
```output
112
112
World
113
-
{{< /output >}}
113
+
```
114
114
115
115
So, Python code can operate on variables just as it does directly on various data. Extending on the example above, here the string variable gets combined (or concatenated) with other strings, using the `+` operator's special role for strings. This allows you to use Python variables in strings:
116
116
117
-
```python
117
+
```command
118
118
example_variable = "World"
119
119
120
120
print("Hello, " + example_variable + "!")
121
121
```
122
122
123
-
{{< output >}}
123
+
```output
124
124
Hello, World!
125
-
{{< /output >}}
125
+
```
126
126
127
127
Similarly, variables can be used in number operations. Variables can even be assigned values based on number operations and also other operations.
Like in many programming languages, Python variables have specific scopes. A variable's scope defines where in the code a variable can be accessed from. Usually, a variable's scope depends on where in the code the variable was declared/assigned, and this is exactly the case in Python.
151
+
Like in many programming languages, Python variables have specific scopes. A variable's scope defines where in the code a variable can be accessed from. Usually, a variable's scope depends on where in the code the variable was declared/assigned, and this is the case in Python.
152
152
153
153
Variables have two different scopes in Python:
154
154
@@ -158,7 +158,7 @@ Variables have two different scopes in Python:
158
158
159
159
To illustrate, the following is an example that makes use of a global variable and two local variables. Follow along with the comments in the code for explanations of each part:
# Global variable, declared at the "top level," outside of any function or loop.
163
163
global_variable = "Hello, "
164
164
@@ -184,24 +184,24 @@ def some_function():
184
184
185
185
## Variable Types
186
186
187
-
The examples above show how variables can vary in their data types. For instance, `example_variable_one` and `example_variable_two` in the [Working with Variables](/docs/guides/python-variables/#working-with-variables) section above use the *integer* data type. Meanwhile, `local_variable_one` and `local_variable_two` in the [Variable Scope](/docs/guides/python-variables/#variable-scope) section use the *string* data type.
187
+
The examples above show how variables can vary in their data types. For instance, `example_variable_one` and `example_variable_two` in the [Working with Variables](#working-with-variables) section above use the *integer* data type. Meanwhile, `local_variable_one` and `local_variable_two` in the [Variable Scope](#variable-scope) section use the *string* data type.
188
188
189
189
Python is a dynamically-typed language, which means, in part, that you do not have to explicitly indicate the variable type. The program automatically, and dynamically infers the variable type. Nevertheless, understanding variables' types are crucial for making sure your code performs as desired.
190
190
191
191
Consider the following example. Running this code produces a type error. Why is that? It is because the code attempts to combine `example_variable_one`, a string, and `example_variable_two`, an integer. Each of these data types uses the `+` operator, but not for operations across data types.
TypeError: can only concatenate str (not "int") to str
204
-
{{< /output >}}
204
+
```
205
205
206
206
There are many more data types than integers and strings, and much more to know about them. To keep learning more, take a look at our [The Basics of Python Data Types](/docs/guides/python-data-types/) guide.
207
207
@@ -211,23 +211,21 @@ There is a way to easily remedy the code example shown above. You can *cast* man
211
211
212
212
The guide to Python data types linked above covers casting in more detail and depth. But, for now, you can see how to use it to effectively handle interactions between integers, and strings. This is perhaps the most common use casefor casting.
213
213
214
-
The following rework of the example code above to use casting. The `str` method casts the integer variable, `example_variable_two`, as a string. Doing so allows number variables to be used as strings.
214
+
The following rework of the example code above uses casting. The `str` method casts the integer variable, `example_variable_two`, as a string. Doing so allows number variables to be used as strings.
This guide has covered the foundations you need to start working with variables in Python. Everything from variable assignment through variable operations, types, and scopes. And with that, you have what you need to start making dynamic Python programs.
230
-
231
-
With that foundation, you may also be looking to move further with your Python skills. In addition to [The Basics of Python Data Types](/docs/guides/python-data-types/) guide linked above, we have numerous other [guides on Python development](/docs/guides/development/python/). These can give you tools to elevate your Python skills and start making your Python code more effective.
229
+
This guide has covered the foundations you need to start working with variables in Python, including variable assignment, variable operations, types, and scopes.
232
230
233
-
Have more questions or want some help getting started? Feel free to reach out to our [Support](https://www.linode.com/support/) team.
231
+
In addition to this guide, you may be interested in [The Basics of Python Data Types](/docs/guides/python-data-types/), or our other [guides on Python development](/docs/guides/development/python/). These can give you tools to elevate your Python skills and start making your Python code more effective.
0 commit comments