DEV Community

Shreya Princy
Shreya Princy

Posted on

Python Variables, Print

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"

  1. 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)

  1. 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}")

  1. 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)

  1. How do you print three lines of text with the strings “Line1”, “Line2”, and “Line3” on separate lines?

print("Line1\nLine2\nLine3")

  1. How do you print the string He said, "Hello, world!" including the double quotes?

print('He said, "Hello, world!"')

  1. How do you print the string C:\Users\Name without escaping the backslashes?

print(r"C:\Users\Name")

  1. How do you print the result of the expression 5 + 3?
    print(5 + 3)

  2. How do you print the strings “Hello” and “world” separated by a hyphen -?

print("Hello-world")

  1. How do you print the string “Hello” followed by a space, and then print “world!” on the same line?

print("Hello", end=" ")
print("world!")

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

is_active = True
print(is_active)

  1. How do you print the string “Hello ” three times in a row?

print("Hello " * 3)

  1. 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.")

  1. 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))

  1. 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}")

  1. 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

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

name = "Shiny"
print(name)

  1. 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)

  1. 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)

  1. 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)

  1. Define constants PI with appropriate values and print them.

PI = 3.14159
print(PI)

  1. 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)

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

LENGTH = 10
WIDTH = 5
area = LENGTH * WIDTH
print(area)

  1. 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

  1. Create a list of five delivery items and print the third item in the list.

items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(items[2])

  1. Add “Glue Stick” to the end of the list and print the updated list.

items.append("Glue Stick")
print(items)

  1. Insert “Highlighter” between the second and third items and print the updated list.

items.insert(2, "Highlighter")
print(items)

  1. Remove “Ruler” from the list and print the updated list.

items.remove("Ruler")
print(items)

  1. Print a sublist containing only the first three items.

print(items[:3])

  1. Convert all item names to uppercase using list comprehension and print.

upper_items = [item.upper() for item in items]
print(upper_items)

  1. Check if “Marker” is in the list and print a message.

print("Marker found" if "Marker" in items else "Marker not found")

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

print(len(items))

  1. Sort the list in alphabetical order and print it.

items.sort()
print(items)

  1. Reverse the list and print it.

items.reverse()
print(items)

  1. 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])

  1. Count how many times “Ruler” appears in the list and print.

print(items.count("Ruler"))

  1. Find the index of “Pencil” in the list and print it.

print(items.index("Pencil"))

  1. Extend the list with another list and print updated list.

new_items = ["Pen", "Sharpener"]
items.extend(new_items)
print(items)

  1. Clear the list and print it.

items.clear()
print(items)

  1. Create a list with “Notebook” repeated three times and print.

repeat_list = ["Notebook"] * 3
print(repeat_list)

  1. 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)

  1. Filter items containing letter “e” and print.

filtered = [item for item in items if "e" in item.lower()]
print(filtered)

  1. 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)