mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-29 21:41:00 +08:00
[手动选题][tech]: 20230317.0 ⭐️ Variables in Python Concepts with Examples.md
This commit is contained in:
parent
a55b89d4f6
commit
1d1403705f
@ -0,0 +1,166 @@
|
|||||||
|
[#]: subject: "Variables in Python: Concepts with Examples"
|
||||||
|
[#]: via: "https://www.debugpoint.com/python-variables/"
|
||||||
|
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
|
||||||
|
[#]: collector: "lkxed"
|
||||||
|
[#]: translator: " "
|
||||||
|
[#]: reviewer: " "
|
||||||
|
[#]: publisher: " "
|
||||||
|
[#]: url: " "
|
||||||
|
|
||||||
|
Variables in Python: Concepts with Examples
|
||||||
|
======
|
||||||
|
|
||||||
|
**This tutorial explains the concept of variables in Python, their types, and how to use them with examples in real-world projects.**
|
||||||
|
|
||||||
|
In Python, a variable is a reserved memory location that stores a value.
|
||||||
|
|
||||||
|
They are names that can be assigned a value and used to reference it throughout your code. Using a variable makes a value accessible & gives values a context/meaning concerning your code.
|
||||||
|
|
||||||
|
Before you start, I hope you have Python installed and set up a basic editor. If not, do refer to my below guides:
|
||||||
|
|
||||||
|
- [How to Install Python 3.11 on Ubuntu/Linux][1]
|
||||||
|
- [How to install Python on Windows][2]
|
||||||
|
- [How to set up the IDLE editor for Python][3]
|
||||||
|
|
||||||
|
### Variable rules
|
||||||
|
|
||||||
|
- Variables are case-sensitive.
|
||||||
|
- Variable names can only contain uppercase and lowercase letters (A–Z, a–z), digits (0–9), and underscores (_).
|
||||||
|
- They can not start with digits.
|
||||||
|
- Python variables are [dynamically typed][4].
|
||||||
|
- Python supports Unicode variables (e.g. decorated letters like é and ü, and even Chinese, Japanese, and Arabic symbols).
|
||||||
|
- As per [PEP 8 standard][5], variable names can be only lowercase, and words can be separated by underscores (e.g. total_price).
|
||||||
|
|
||||||
|
### Python Variables: Examples
|
||||||
|
|
||||||
|
To create a variable in Python, we need to assign a value to it using the assignment operator (=). For example, the following line of code creates a variable named “x” and assigns it the value of 10:
|
||||||
|
|
||||||
|
```
|
||||||
|
x = 10
|
||||||
|
```
|
||||||
|
|
||||||
|
In Python, variables are dynamically typed, which means that the interpreter can determine the data type of a variable based on the value it is assigned. Python supports various types of variables, including integers, floats, strings, booleans, and complex numbers.
|
||||||
|
|
||||||
|
```
|
||||||
|
# Integer Variable
|
||||||
|
age = 20
|
||||||
|
|
||||||
|
# Float Variable
|
||||||
|
price = 4.99
|
||||||
|
|
||||||
|
# String Variable
|
||||||
|
name = "John Doe"
|
||||||
|
|
||||||
|
# Boolean Variable
|
||||||
|
is_active = True
|
||||||
|
|
||||||
|
# Complex Variable
|
||||||
|
z = 2 + 3j
|
||||||
|
```
|
||||||
|
|
||||||
|
Variables are useful in programming because they allow us to store and manipulate data. For example, we can use variables to perform mathematical operations, concatenate strings, and make decisions based on the value of a boolean variable.
|
||||||
|
|
||||||
|
```
|
||||||
|
# Mathematical Operations
|
||||||
|
x = 5
|
||||||
|
y = 10
|
||||||
|
z = x + y
|
||||||
|
print(z) # Output: 15
|
||||||
|
|
||||||
|
# String Concatenation
|
||||||
|
first_name = "John"
|
||||||
|
last_name = "Doe"
|
||||||
|
full_name = first_name + " " + last_name
|
||||||
|
print(full_name) # Output: John Doe
|
||||||
|
```
|
||||||
|
|
||||||
|
![A simple demonstration of variables in Python][6]
|
||||||
|
|
||||||
|
Variables can also be used in real-world projects to store and manipulate data. For example, in a web application, we can use variables to [store user input][7], database queries, and output data to the user.
|
||||||
|
|
||||||
|
```
|
||||||
|
# User Input
|
||||||
|
name = input("What is your name? ")
|
||||||
|
print("Hello, " + name + "!") # Output: Hello, John!
|
||||||
|
|
||||||
|
# Database Queries
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
conn = sqlite3.connect("example.db")
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
cursor.execute("SELECT * FROM users WHERE id = ?", (1,))
|
||||||
|
user = cursor.fetchone()
|
||||||
|
|
||||||
|
print(user) # Output: (1, 'John Doe', 'johndoe@example.com')
|
||||||
|
|
||||||
|
# Output Data to User
|
||||||
|
balance = 100.00
|
||||||
|
print("Your current balance is ₹" + str(balance)) # Output: Your current balance is ₹100.0
|
||||||
|
```
|
||||||
|
|
||||||
|
### Common Errors while using variables
|
||||||
|
|
||||||
|
There are a few common errors you can face while dealing with variables. Here are some of them.
|
||||||
|
|
||||||
|
- `NameError`: This error occurs when you try to access a variable that has not been defined. For example, if you try to print the value of a variable that has not been assigned a value yet, you will get a NameError. The following code gives a NameError because the variable “Full_name” is not defined.
|
||||||
|
|
||||||
|
```
|
||||||
|
# NameError demonstration
|
||||||
|
first_name = "John"
|
||||||
|
last_name = "Doe"
|
||||||
|
full_name = first_name + " " + last_name
|
||||||
|
print(Full_name) # NameError
|
||||||
|
```
|
||||||
|
|
||||||
|
![NameError example][8]
|
||||||
|
|
||||||
|
- `TypeError`: This error occurs when you try to operate on a variable of the wrong data type. For example, if you try to concatenate a string and an integer, you will get a TypeError. The below snippet of code gives TypeError.
|
||||||
|
|
||||||
|
```
|
||||||
|
# TypeError demonstration
|
||||||
|
first_name = "John"
|
||||||
|
age = 10
|
||||||
|
print(first_name + age)
|
||||||
|
```
|
||||||
|
|
||||||
|
![TypeError example in Python][9]
|
||||||
|
|
||||||
|
- `ValueError`: This error occurs when you try to convert a variable to a different data type, but the conversion is impossible. For example, if you try to convert a string that contains letters to an integer, you will get a ValueError.
|
||||||
|
|
||||||
|
```
|
||||||
|
# ValueError demonstration
|
||||||
|
first_name = "John"
|
||||||
|
age = 10
|
||||||
|
print(int(first_name))
|
||||||
|
```
|
||||||
|
|
||||||
|
![ValueError in Python][10]
|
||||||
|
|
||||||
|
### Wrapping Up
|
||||||
|
|
||||||
|
Understanding variables is essential for any Python developer. Variables allow us to store and manipulate data, perform mathematical operations, concatenate strings, etc. I hope this guide clarifies the concept if you are a beginner in Python programming.
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.debugpoint.com/python-variables/
|
||||||
|
|
||||||
|
作者:[Arindam][a]
|
||||||
|
选题:[lkxed][b]
|
||||||
|
译者:[译者ID](https://github.com/译者ID)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://www.debugpoint.com/author/admin1/
|
||||||
|
[b]: https://github.com/lkxed/
|
||||||
|
[1]: https://www.debugpoint.com/install-python-3-11-ubuntu/
|
||||||
|
[2]: https://www.debugpoint.com/install-python-windows/
|
||||||
|
[3]: https://www.debugpoint.com/install-idle-ubuntu-linux/
|
||||||
|
[4]: https://en.wikipedia.org/wiki/Type_system
|
||||||
|
[5]: https://peps.python.org/pep-0008/
|
||||||
|
[6]: https://www.debugpoint.com/wp-content/uploads/2023/03/A-simple-demonstration-of-variables-in-Python.jpg
|
||||||
|
[7]: https://www.debugpoint.com/input-function-python/
|
||||||
|
[8]: https://www.debugpoint.com/wp-content/uploads/2023/03/NameError-example.jpg
|
||||||
|
[9]: https://www.debugpoint.com/wp-content/uploads/2023/03/TypeError-example-in-Python.jpg
|
||||||
|
[10]: https://www.debugpoint.com/wp-content/uploads/2023/03/ValueError-in-Python.jpg
|
Loading…
Reference in New Issue
Block a user