Skip to content

Commit ba6fb3b

Browse files
python variables copy edit
1 parent ff9e6b2 commit ba6fb3b

File tree

1 file changed

+43
-45
lines changed
  • docs/guides/development/python/python-variables

1 file changed

+43
-45
lines changed

docs/guides/development/python/python-variables/index.md

+43-45
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,55 @@
11
---
22
slug: python-variables
3-
author:
4-
name: Linode Community
5-
email: docs@linode.com
63
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."
74
keywords: ['python variables', 'python variables naming convention', 'python variables case sensitive']
85
tags: ['python']
96
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
10-
published: 2022-04-27
7+
published: 2023-04-03
118
modified_by:
129
name: Nathaniel Stickman
1310
title: "Getting Started with Python Variables"
14-
h1_title: "Getting Started with Python Variables"
15-
enable_h1: true
16-
contributor:
17-
name: Nathaniel Stickman
18-
link: https://github.com/nasanos
11+
title_meta: "Getting Started with Python Variables"
1912
external_resources:
2013
- '[W3 Schools: Python Variables](https://www.w3schools.com/python/python_variables.asp)'
2114
- '[Real Python: Variables in Python](https://realpython.com/python-variables/)'
2215
- '[GeeksforGeeks: Python Variables](https://www.geeksforgeeks.org/python-variables/)'
2316
- '[Tutorials Point: Python - Variable Types](https://www.tutorialspoint.com/python/python_variable_types.htm)'
17+
authors: ["Nathaniel Stickman"]
2418
---
2519

26-
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.
2721

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.
2923

3024
## Assigning Variables
3125

3226
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:
3327

34-
``` python
28+
```command
3529
example_variable = "This is an example string variable."
3630
```
3731

3832
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:
3933

40-
``` python
34+
```command
4135
example_variable = 5
4236
```
4337

4438
You can also assign multiple variables simultaneously using commas to separate the variables and their respective values, like so:
4539

46-
``` python
40+
```command
4741
variable_one, variable_two, variable_three = 27, 8.3, "Example"
4842

4943
print(variable_one)
5044
print(variable_two)
5145
print(variable_three)
5246
```
5347

54-
{{< output >}}
48+
```output
5549
27
5650
8.3
5751
Example
58-
{{< /output >}}
52+
```
5953

6054
### Naming Conventions
6155

@@ -77,18 +71,24 @@ Python variables support three popular naming conventions:
7771

7872
- Camel case, where the first letter is lowercase and each subsequent word in a name begins with an uppercase letter.
7973

80-
exampleVariable = "This is a string variable."
81-
anotherExampleVariable = 31
74+
```command
75+
exampleVariable = "This is a string variable."
76+
anotherExampleVariable = 31
77+
```
8278

8379
- Snake case, where separate words are separated by underscores.
8480

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+
```
8785

8886
- Pascal case, where each word in a variable name begins with an uppercase letter.
8987

90-
ExampleVariable = "This is a string variable."
91-
AnotherExampleVariable = 31
88+
```command
89+
ExampleVariable = "This is a string variable."
90+
AnotherExampleVariable = 31
91+
```
9292

9393
Python does not have an official naming convention, so you are free to choose between these.
9494

@@ -102,31 +102,31 @@ However, if you are looking to align with more of the Python community, you may
102102

103103
Variables, once assigned, can be used almost anywhere within your Python code. For instance, the `print` function takes a string as an argument. As the example below shows, a variable containing a string can be substituted in place of a plain string.
104104

105-
``` python
105+
```command
106106
example_variable = "World"
107107
108108
print(example_variable)
109109
```
110110

111-
{{< output >}}
111+
```output
112112
World
113-
{{< /output >}}
113+
```
114114

115115
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:
116116
117-
``` python
117+
```command
118118
example_variable = "World"
119119
120120
print("Hello, " + example_variable + "!")
121121
```
122122
123-
{{< output >}}
123+
```output
124124
Hello, World!
125-
{{< /output >}}
125+
```
126126
127127
Similarly, variables can be used in number operations. Variables can even be assigned values based on number operations and also other operations.
128128
129-
``` python
129+
```command
130130
example_variable_one = 9
131131
example_variable_two = 4
132132
@@ -140,15 +140,15 @@ print(another_example_variable_two)
140140
print(last_example_variable)
141141
```
142142
143-
{{< output >}}
143+
```output
144144
27
145145
2
146146
29
147-
{{< /output >}}
147+
```
148148
149149
## Variable Scope
150150
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 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.
152152
153153
Variables have two different scopes in Python:
154154
@@ -158,7 +158,7 @@ Variables have two different scopes in Python:
158158
159159
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:
160160
161-
``` python
161+
```file {title="python_variable_scope.py" lang="python"}
162162
# Global variable, declared at the "top level," outside of any function or loop.
163163
global_variable = "Hello, "
164164
@@ -184,24 +184,24 @@ def some_function():
184184
185185
## Variable Types
186186
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.
188188
189189
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.
190190

191191
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.
192192

193-
``` python
193+
```command
194194
example_variable_one = "Hello, "
195195
example_variable_two = 5
196196
197197
print(example_variable_one + example_variable_two)
198198
```
199199

200-
{{< output >}}
200+
```output
201201
Traceback (most recent call last):
202202
File "<stdin>", line 1, in <module>
203203
TypeError: can only concatenate str (not "int") to str
204-
{{< /output >}}
204+
```
205205

206206
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.
207207

@@ -211,23 +211,21 @@ There is a way to easily remedy the code example shown above. You can *cast* man
211211

212212
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 case for casting.
213213

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.
215215

216-
``` python
216+
```command
217217
example_variable_one = "Hello, "
218218
example_variable_two = 5
219219
220220
print(example_variable_one + str(example_variable_two) + "!")
221221
```
222222

223-
{{< output >}}
223+
```output
224224
Hello, 5!
225-
{{< /output >}}
225+
```
226226

227227
## Conclusion
228228

229-
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.
232230

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

Comments
 (0)