Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Variables and Simple Data Storage | Everyday Calculations and Automation
Python for Daily Tasks

bookVariables and Simple Data Storage

Swipe um das Menü anzuzeigen

Variables are one of the most important tools in Python for storing and reusing information. A variable acts like a labeled container where you can keep a value, such as a number, a piece of text, or other data. To create a variable, you use the assignment operator =, which stores a value in the variable on the left side of the operator. Naming your variables clearly is important for understanding your code later. In Python, variable names must start with a letter or an underscore (_), can only contain letters, numbers, and underscores, and are case-sensitive. For example, rent, monthly_budget, and _utilities are all valid variable names, but 2ndItem or total-cost are not.

123456789
# Assigning values to variables for a monthly budget rent = 1200 groceries = 350 utilities = 150 transportation = 75 # Calculate the total monthly budget total_budget = rent + groceries + utilities + transportation print("Total monthly budget:", total_budget)
copy

Once you have stored information in variables, you can update their values as your plans or needs change. This is especially useful for daily planning, because you often need to adjust numbers as new information comes in. For example, if your utility bill is higher than expected, you can update the utilities variable and recalculate your total budget easily. Using variables also helps you avoid repeating the same value in multiple places, making your code easier to maintain and adapt to changes.

1234
# Modifying a budget by updating variable values and recalculating totals utilities = 180 # Updated utility bill total_budget = rent + groceries + utilities + transportation print("Updated total monthly budget:", total_budget)
copy

1. Why are variables useful in programming?

2. Which of the following is a valid variable name in Python?

3. Fill in the blanks to assign and update a variable for tracking daily steps.

question mark

Why are variables useful in programming?

Wählen Sie alle richtigen Antworten aus

question mark

Which of the following is a valid variable name in Python?

Wählen Sie die richtige Antwort aus

question-icon

Fill in the blanks to assign and update a variable for tracking daily steps.

# Update steps_walked by adding 2000 more steps steps_walked = steps_walked + print("Total steps walked today:", steps_walked)
Total steps walked today: 7000

Klicken oder ziehen Sie Elemente und füllen Sie die Lücken aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 2
some-alt