Python Exercise

Introduction

Welcome to this Python exercise! In this exercise, you will work with basic Python operations such as performing arithmetic, and working with variables.

Task 1: Add, Subtract, and Multiply Two Numbers

Description:

Write a program that defines two numbers and calculates their sum, difference, and product.

Steps:

  1. Define two variables: number1 and number2.
  2. Assign any integer value to the variables
  3. Add, subtract, and multiply these two numbers.
  4. Print the results of the calculations.

Task 2: Variables and Types

Description:

In this task, you will define different types of variables, display their values, sum them, and identify their types.

Instructions:

  1. Define three variables: zahl1, zahl2, and text.
  2. Assign any integer value to zahl1, any floating-point value to zahl2, and any string to text.
  3. Display the values of zahl1, zahl2, and text.
  4. Calculate the sum of zahl1 and zahl2 and display the result.
  5. Print the data type of zahl1, zahl2, and text.

Task 3: String Manipulation

Description:

In this task, you will perform various operations on strings, such as concatenation, length calculation, replacement, finding characters, splitting, counting characters, changing cases, and extracting substrings.

Instructions:

  1. Define two strings: string1 and string2.
  2. Concatenate the two strings and print the result.
  3. Calculate the length of the concatenated string and print the result.
  4. Replace all occurrences of a specific character in string1 with another character and print the result.
  5. Find the position of a specific character in string2 and print the result.
  6. Split string2 into multiple lines and print the result.
  7. Count how many times a specific character appears in string1 and print the result.
  8. Convert string1 to uppercase and lowercase, and print both results.
  9. Extract a substring from string1 and print the result.

Task 4: Replace Characters in a String Using Input

Description:

Write a program that asks the user for a sentence and two characters. Replace all occurrences of the first character with the second character in the sentence and print the result.

Instructions:

  1. Use the input function to ask the user for a sentence.
  2. Use the input function to ask the user for two characters: one to be replaced and one to replace it.
  3. Use the replace method to replace all occurrences of the first character in the sentence with the second character.
  4. Print the new sentence.

Happy coding!

Updated: