❌

Reading view

There are new articles available, click to refresh the page.

Task 6: Tuple

1.Create a tuple containing the names of three fruits. Print the tuple and its type.

Image description

2.Given a tuple t = ("apple", "banana", "cherry"), access and print the second element.

Image description

3.Unpack the tuple t = (1, 2, 3) into variables a, b, and c, and print the variables.

Image description

4.Concatenate two tuples t1 = (1, 2) and t2 = (3, 4) and print the result.

Image description

5.Create a tuple t = ("repeat",) and repeat it three times. Print the resulting tuple.

Image description

6.Given a tuple t = (1, 2, 3, 2, 2, 4), count the number of times the number 2 appears.

Image description

7.Given a tuple t = ("a", "b", "c", "d"), find the index of the element "c".

Image description

8.Check if the element 5 is present in the tuple t = (1, 2, 3, 4)

Image description

9.Find and print the length of the tuple t = ("one", "two", "three").

Image description

10.Slice the tuple t = (0, 1, 2, 3, 4, 5) to obtain a sub-tuple containing only the elements from index 2 to 4.

Image description

11.Create a nested tuple representing a 2D point (x, y) = ((1, 2), (3,
4)). Access and print the second coordinate of the second point.

Image description

12.Try to change the value of the first element in the tuple t = (1, 2, 3) and observe what happens.

Image description

13.Convert a list l = [1, 2, 3] to a tuple and print the result. Then convert a tuple t = (4, 5, 6) to a list and print the result.

Image description

14.Create a tuple with a single item 5 and verify its type is a tuple.

Image description

15.Iterate over the tuple t = ("ParottaSalna", "is", "good") and print each element.
16.Convert the string "hello" into a tuple of characters.
17.Convert a dictionary d = {"one": 1, "two": 2} into a tuple of its items.
18.Write a function that takes a tuple of numbers and returns the sum of the numbers.
19.Use tuples as keys in a dictionary to represent points on a grid. For example, grid = {(0, 0): "origin", (1, 2): "point A"}.

Task 5: Python Function

1.Write a function greet that takes a name as an argument and prints a greeting message.

Image description

2.Write a function sum_two that takes two numbers as arguments and returns their sum.

Image description

3.Write a function is_even that takes a number as an argument and returns True if the number is even, and False if it is odd.

Image description

4.Write a function find_max that takes two numbers as arguments and returns the larger one.

Image description

5.Write a function multiplication_table that takes a number n and prints the multiplication table for n from 1 to 10.

Image description

Image description

6.Write a function celsius_to_fahrenheit that takes a temperature in Celsius and returns the temperature in Fahrenheit.

Image description

7.Write a function power that takes two arguments, a number and an exponent, and returns the number raised to the given exponent. The exponent should have a default value of 2.

Image description

TASK 4: Python Lists

1.Create a list of five delivery items and print the third item in the list. eg: [β€œNotebook”, β€œPencil”, β€œEraser”, β€œRuler”, β€œMarker”]

Image description

2.A new delivery item β€œGlue Stick” needs to be added to the list. Add it to the end of the list and print the updated list.

Image description

3.Insert β€œHighlighter” between the second and third items and print the updated list.

Image description

4.One delivery was canceled. Remove β€œRuler” from the list and print the updated list.

Image description

5.The delivery man needs to deliver only the first three items. Print a sublist containing only these items.

Image description

6.The delivery man has finished his deliveries. Convert all item names to uppercase using a list comprehension and print the new list.

Image description

7.Check if β€œMarker” is still in the list and print a message indicating whether it is found.

Image description

8.Print the number of delivery items in the list.

Image description

9.Sort the list of items in alphabetical order and print the sorted list.

Image description

10.The delivery man decides to reverse the order of his deliveries. Reverse the list and print it.

Image description

11.Create a list where each item is a list containing a delivery item and its delivery time. Print the first item and its time.

12.Count how many times β€œRuler” appears in the list and print the count.

Image description

13.Find the index of β€œPencil” in the list and print it.

Image description

14.Extend the list items with another list of new delivery items and print the updated list.

Image description

15.Clear the list of all delivery items and print the list.

Image description

16.Create a list with the item β€œNotebook” repeated three times and print the list.

Image description

17.Using a nested list comprehension, create a list of lists where each sublist contains an item and its length, then print the new list.

Image description

18.Filter the list to include only items that contain the letter β€œe” and print the filtered list.

Image description

19.Remove duplicate items from the list and print the list of unique items.

Image description

TASK 3: Slicing & Indexing

1.Write a function that takes a string and returns a new string consisting of its first and last character.

Image description

2.Write a function that reverses a given string.

Image description

3.Given a string, extract and return a substring from the 3rd to the 8th character (inclusive).

Image description

4.Write a function that returns every second character from a given string.

Image description

5.Write a function that replaces the middle third of a string with asterisks. If the length of the string is not divisible by 3, adjust the middle third accordingly.

6.Write a function that checks if a given string is a palindrome (reads the same backward as forward).

7.Given an email address, extract and return the domain.
Ans: How to find incase there are multiple occurrences? Loop?
Image description

Image description

8.Write a function that returns every third character from a given string.

Image description

9.Write a function that extracts and returns characters at even indices from a given string.

Image description

10.Write a function that skips every second character and then reverses the resulting string.

Image description

TASK 2: Constants and Variables

1.Create a variable named name and assign your name to it. Then print the value of the variable.

Image description

2.Create a variable age and assign your age to it. Later, reassign the variable with a new value and print the new value.

Image description

3.Assign the values 5, 10, and 15 to three variables a, b, and c in a single line. Print their values.
Ans:TypeError on 4th statement, because we're trying to concatenate integers (a, b, c) with strings

Image description

4.Swap the values of two variables x and y without using a third variable. Print their values before and after swapping.

Image description

5.Define constants PI with appropriate values and print them.
Ans: constants are typically written in all uppercase letters with underscores separating words. However, Python does not enforce this, so constants are not truly immutable. You can still override.

Image description

6.Write a program that calculates the area of a circle using the constant PI and a variable radius. Print the area.
Ans: ** is Squared

Image description

7.Define constants for the length and width of a rectangle. Calculate and print the area.

Image description

8.Define a constant for Ο€ (pi) and a variable for the radius. Calculate and print the circumference of the circle.

Image description

TASK 1: Python – Print exercises

1.How do you print the string β€œHello, world!” to the screen?
Ans: Using sep and end Parameters is preferred way.
Image description

2.How do you print the value of a variable name which is set to β€œSyed Jafer” or Your name?
Ans: Note the Variable is case sensitive

Image description

3.How do you print the variables name, age, and city with labels β€œName:”, β€œAge:”, and β€œCity:”?
Ans: Using keyword sep="," brings in , between the Variable Name and Value itself, so avoid

Image description

4.How do you use an f-string to print name, age, and city in the format β€œName: …, Age: …, City: …”?
Ans: To insert variables directly into the string used f-string
Also, you can assign values to multiple variables in a single line as seen in 1st line

Image description

5.How do you concatenate and print the strings greeting (β€œHello”) and target (β€œworld”) with a space between them?
Ans: + is used to concat the items

Image description

6.How do you print three lines of text with the strings β€œLine1”, β€œLine2”, and β€œLine3” on separate lines?****

Image description

7.How do you print the string He said, "Hello, world!" including the double quotes?
Ans: To print quotes inside a string, you can use either single or double quotes to enclose the string and the other type of quotes inside it.

Image description

8.How do you print the string C:\Users\Name without escaping the backslashes?
Ans: you can also use a literal backslash "\" when using Concat or Try with 'r' to treat backslashes as literal characters

Image description

9.How do you print the result of the expression 5 + 3?

Image description

10.How do you print the strings β€œHello” and β€œworld” separated by a hyphen -?

Image description

11.How do you print the string β€œHello” followed by a space, and then print β€œworld!” on the same line?

Image description

12.How do you print the value of a boolean variable is_active which is set to True?

Image description

13.How do you print the string β€œHello ” three times in a row?

Image description

14.How do you print the sentence The temperature is 22.5 degrees Celsius. using the variable temperature?

Image description

15.How do you print name, age, and city using the .format() method in the format β€œName: …, Age: …, City: …”?

Image description

16.How do you print the value of pi (3.14159) rounded to two decimal places in the format The value of pi is approximately 3.14?
Ans: pi is the variable & .2f formats it as a floating-point number with 2 digits after the decimal

Image description

17.How do you print the words β€œleft” and β€œright” with β€œleft” left-aligned and β€œright” right-aligned within a width of 10 characters each?

Image description

❌