Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Useful Converting | Bring All the Topics Together
Data Types in Python

Swipe um das Menü anzuzeigen

book
Useful Converting

The int() function can be useful if you're working with numerical data. For instance, imagine that we have a lot of numbers that need to be modified, but all of them have a string data type. This code leads to an error:

1234
value = "123" new_value = value + 10 print(new_value)
copy

Here, Python tries to concatenate the strings, but indeed its functionality doesn't allow it to do so, since 10 is related to the integer data type (concatenation can be applied only to strings).

To correctly sum these numbers, you should initially transform the string into an integer using int() function:

1234
value = "123" new_value = int(value) + 10 print(new_value)
copy
Aufgabe

Swipe to start coding

You're working on a small e-commerce platform. Users submit product prices through a form — but all the values are passed as strings.

Before saving the data or performing calculations, you need to convert the prices into integers, adjust them, and store the updated values in new variables.

  1. Convert the string value of price_laptop to an integer, add 15, and store the result in new_price_laptop.
  2. Convert the string value of price_headphones to an integer, add 780, and store the result in new_price_headphones.
  3. Convert the string value of price_monitor to an integer, subtract 90, and store the result in new_price_monitor.

All final results must be stored as int values.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
Wir sind enttäuscht, dass etwas schief gelaufen ist. Was ist passiert?

Fragen Sie AI

expand
ChatGPT

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

book
Useful Converting

The int() function can be useful if you're working with numerical data. For instance, imagine that we have a lot of numbers that need to be modified, but all of them have a string data type. This code leads to an error:

1234
value = "123" new_value = value + 10 print(new_value)
copy

Here, Python tries to concatenate the strings, but indeed its functionality doesn't allow it to do so, since 10 is related to the integer data type (concatenation can be applied only to strings).

To correctly sum these numbers, you should initially transform the string into an integer using int() function:

1234
value = "123" new_value = int(value) + 10 print(new_value)
copy
Aufgabe

Swipe to start coding

You're working on a small e-commerce platform. Users submit product prices through a form — but all the values are passed as strings.

Before saving the data or performing calculations, you need to convert the prices into integers, adjust them, and store the updated values in new variables.

  1. Convert the string value of price_laptop to an integer, add 15, and store the result in new_price_laptop.
  2. Convert the string value of price_headphones to an integer, add 780, and store the result in new_price_headphones.
  3. Convert the string value of price_monitor to an integer, subtract 90, and store the result in new_price_monitor.

All final results must be stored as int values.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
Wir sind enttäuscht, dass etwas schief gelaufen ist. Was ist passiert?
some-alt