Variable python что это

Python Variables

A variable is created the moment you first assign a value to it.

Example

Variables do not need to be declared with any particular type, and can even change type after they have been set.

Example

Casting

If you want to specify the data type of a variable, this can be done with casting.

Example

Get the Type

You can get the data type of a variable with the type() function.

Example

Single or Double Quotes?

String variables can be declared either by using single or double quotes:

Example

Case-Sensitive

Variable names are case-sensitive.

Example

This will create two variables:

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Python Variables

Summary: in this tutorial, you’ll learn about Python variables and how to use them effectively.

What is a variable in Python

When you develop a program, you need to manage values, a lot of them. To store values, you use variables.

In Python, a variable is a label that you can assign a value to it. And a variable is always associated with a value. For example:

message = 'Hello, World!' print(message) message = 'Good Bye!' print(message)Code language: Python (python)
Hello, World! Good Bye!Code language: Python (python)

In this example, message is a variable. It holds the string ‘Hello, World!’ . The print() function shows the message Hello, World! to the screen.

The next line assigns the string ‘Good Bye!’ to the message variable and print its value to the screen.

The variable message can hold various values at different times. And its value can change throughout the program.

Creating variables

To define a variable, you use the following syntax:

variable_name = valueCode language: Python (python)

The = is the assignment operator. In this syntax, you assign a value to the variable_name .

The value can be anything like a number, a string, etc., that you assign to the variable.

The following defines a variable named counter and assigns the number 1 to it:

counter = 1Code language: Python (python)

Naming variables

When you name a variable, you need to adhere to some rules. If you don’t, you’ll get an error.

The following are the variable rules that you should keep in mind:

  • Variable names can contain only letters, numbers, and underscores ( _ ). They can start with a letter or an underscore ( _ ), not with a number.
  • Variable names cannot contain spaces. To separate words in variables, you use underscores for example sorted_list .
  • Variable names cannot be the same as keywords, reserved words, and built-in functions in Python.

The following guidelines help you define good variable names:

  • Variable names should be concise and descriptive. For example, the active_user variable is more descriptive than the au .
  • Use underscores (_) to separate multiple words in the variable names.
  • Avoid using the letter l and the uppercase letter O because they look like the number 1 and 0 .

Summary

  • A variable is a label that you can assign a value to it. The value of a variable can change throughout the program.
  • Use the variable_name = value to create a variable.
  • The variable names should be as concise and descriptive as possible. Also, they should adhere to Python variable naming rules.

Источник

Читайте также:  Http wiki stavcdo ru index php профессиональные объединения учителей ставропольского края
Оцените статью