How do you print the string “Hello, world!” to the screen?
print("Hello, world!")
2.How do you print the value of a variable name which is set to “Syed Jafer” or your name?
name = "Shreya"
- How do you print the variables name, age, and city with labels “Name:”, “Age:”, and “City:”?
name = "Shreya" age = 21 city = "Chennai"
print("Name:", name, "Age:", age, "City:", city)
- How do you use an f-string to print name, age, and city in the format “Name: …, Age: …, City: …”?
print(f"Name: {name}, Age: {age}, City: {city}")
- How do you concatenate and print the strings greeting (“Hello”) and target (“world”) with a space between them?
greeting = "Hello" target = "world"
print(greeting + " " + target)
- How do you print three lines of text with the strings “Line1”, “Line2”, and “Line3” on separate lines?
print("Line1\nLine2\nLine3")
- How do you print the string He said, "Hello, world!" including the double quotes?
print('He said, "Hello, world!"')
- How do you print the string C:\Users\Name without escaping the backslashes?
print(r"C:\Users\Name")
How do you print the result of the expression 5 + 3?
print(5 + 3)How do you print the strings “Hello” and “world” separated by a hyphen -?
print("Hello-world")
- How do you print the string “Hello” followed by a space, and then print “world!” on the same line?
print("Hello", end=" ")
print("world!")
- How do you print the value of a boolean variable is_active which is set to True?
is_active = True
print(is_active)
- How do you print the string “Hello ” three times in a row?
print("Hello " * 3)
- How do you print the sentence The temperature is 22.5 degrees Celsius. using the variable temperature?
temperature = 22.5
print(f"The temperature is {temperature} degrees Celsius.")
- How do you print name, age, and city using the .format() method in the format “Name: …, Age: …, City: …”?
print("Name: {}, Age: {}, City: {}".format(name, age, city))
- 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?
print(f"The value of pi is approximately {pi:.2f}")
- How do you print the words “left” and “right” with “left” left-aligned and “right” right-aligned within a width of 10 characters each?
print(f"{'left':<10}{'right':>10}")
TASK 2
- Create a variable named name and assign your name to it. Then print the value of the variable.
name = "Shiny"
print(name)
- Create a variable age and assign your age to it. Later, reassign the variable with a new value and print the new value.
age = 20
age = 21
print(age)
- Assign the values 5, 10, and 15 to three variables a, b, and c in a single line. Print their values.
a, b, c = 5, 10, 15
print(a, b, c)
- Swap the values of two variables x and y without using a third variable. Print their values before and after swapping. x = 5 y = 10 print("Before:", x, y) x, y = y, x
print("After:", x, y)
- Define constants PI with appropriate values and print them.
PI = 3.14159
print(PI)
- Write a program that calculates the area of a circle using the constant PI and a variable radius. Print the area.
PI = 3.14159
radius = 5
area = PI * radius * radius
print(area)
- Define constants for the length and width of a rectangle. Calculate and print the area.
LENGTH = 10
WIDTH = 5
area = LENGTH * WIDTH
print(area)
- Define a constant for π (pi) and a variable for the radius. Calculate and print the circumference of the circle.
radius = 5 ,circumference = 2 * PI * radius
print(circumference)
TASK 3
- Create a list of five delivery items and print the third item in the list.
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(items[2])
- Add “Glue Stick” to the end of the list and print the updated list.
items.append("Glue Stick")
print(items)
- Insert “Highlighter” between the second and third items and print the updated list.
items.insert(2, "Highlighter")
print(items)
- Remove “Ruler” from the list and print the updated list.
items.remove("Ruler")
print(items)
- Print a sublist containing only the first three items.
print(items[:3])
- Convert all item names to uppercase using list comprehension and print.
upper_items = [item.upper() for item in items]
print(upper_items)
- Check if “Marker” is in the list and print a message.
print("Marker found" if "Marker" in items else "Marker not found")
- Print the number of delivery items in the list.
print(len(items))
- Sort the list in alphabetical order and print it.
items.sort()
print(items)
- Reverse the list and print it.
items.reverse()
print(items)
- Create a list of items with delivery time and print first item and its time.
delivery = [["Notebook", "10AM"], ["Pencil", "11AM"], ["Eraser", "12PM"]]
print(delivery[0])
- Count how many times “Ruler” appears in the list and print.
print(items.count("Ruler"))
- Find the index of “Pencil” in the list and print it.
print(items.index("Pencil"))
- Extend the list with another list and print updated list.
new_items = ["Pen", "Sharpener"]
items.extend(new_items)
print(items)
- Clear the list and print it.
items.clear()
print(items)
- Create a list with “Notebook” repeated three times and print.
repeat_list = ["Notebook"] * 3
print(repeat_list)
- Create a list of lists with item and its length using nested list comprehension.
items = ["Notebook", "Pencil", "Eraser"]
length_list = [[item, len(item)] for item in items]
print(length_list)
- Filter items containing letter “e” and print.
filtered = [item for item in items if "e" in item.lower()]
print(filtered)
- Remove duplicate items and print unique list. items = ["Notebook", "Pencil", "Notebook", "Eraser"] unique_items = list(set(items)) print(unique_items) print(name)
Top comments (0)