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:
- Define two variables:
number1
andnumber2
. - Assign any integer value to the variables
- Add, subtract, and multiply these two numbers.
- 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:
- Define three variables:
zahl1
,zahl2
, andtext
. - Assign any integer value to
zahl1
, any floating-point value tozahl2
, and any string totext
. - Display the values of
zahl1
,zahl2
, andtext
. - Calculate the sum of
zahl1
andzahl2
and display the result. - Print the data type of
zahl1
,zahl2
, andtext
.
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:
- Define two strings:
string1
andstring2
. - Concatenate the two strings and print the result.
- Calculate the length of the concatenated string and print the result.
- Replace all occurrences of a specific character in
string1
with another character and print the result. - Find the position of a specific character in
string2
and print the result. - Split
string2
into multiple lines and print the result. - Count how many times a specific character appears in
string1
and print the result. - Convert
string1
to uppercase and lowercase, and print both results. - 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:
- Use the
input
function to ask the user for a sentence. - Use the
input
function to ask the user for two characters: one to be replaced and one to replace it. - Use the
replace
method to replace all occurrences of the first character in the sentence with the second character. - Print the new sentence.
Happy coding!