Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Variables | Basic Syntax
Introduction to Python with Cursor

bookVariables

メニューを表示するにはスワイプしてください

Variables and Values

A variable is a name that refers to a value in memory. You assign values with the = symbol and can reuse them instead of repeating the same data.

Python automatically detects the type of the value — number, string, or another object — so you don't declare it explicitly. Variables are flexible: you can change their contents or even assign a new value of a different type.

Naming Rules for Variables

Variable names in Python must follow these rules:

  • Start with a letter or underscore;
  • Contain only letters, digits, or underscores;
  • No spaces, special characters, or punctuation;
  • Cannot be Python keywords;
  • Case-sensitive: name and Name are different.

Best practices:

  • Use short but meaningful names, like price or user_age;
  • Use lowercase with underscores: total_sum;
  • Avoid single-letter names, except for simple counters.

Reusability and Why Variables Matter

Variables prevent repetition and make code easier to maintain. They improve readability, reduce mistakes, and simplify updates.

Instead of repeating the same value in several places, you store it in one variable and reuse it. If the value changes, you only update it once.

Summary

  • A variable stores a value using the = symbol;
  • Python automatically detects the data type;
  • You can update or overwrite variables at any time;
  • Variable names must follow syntax rules and naming conventions;
  • Variables improve flexibility, readability, and maintainability in code.
question mark

Which of the following is a valid Python variable name?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  6

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  6
some-alt