How to define variables in python

silencement
Release: 2022-06-07 15:12:10
Original
20166 people have browsed it

How to define variables in python

Variables

A variable stores a value.

Example

message = "Hello Python world!" print(message)
Copy after login

A variable stores a value. You can change this value at any time.

message = "Hello Python world!" print(message) message = "Python is my favorite language!" print(message)
Copy after login

Naming rules

1. Variable names can only contain letters, numbers, and underscores. And it can only start with a letter or underscore.

2. Spaces are not allowed in variable names.

3. You cannot use Python keywords as variable names.

4. Variable names should be meaningful. Not too short or too long. For example: mc_wheels is better than wheels and number_of_wheels_on_a_motorycle.

5. Be careful with lowercase l and uppercase O . They are easily confused with 1 and 0.

NameError

NameError is a common variable error. Read the following code carefully to find out what went wrong:

message = "Thank you for sharing Python with the world, Guido!" print(mesage)
Copy after login

This error is caused by inconsistent variables. We can avoid this error as long as we ensure that the variable names are consistent. As shown below:

message = "Thank you for sharing Python with the world, Guido!" print(message)
Copy after login

The above is the detailed content of How to define variables in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn