0% found this document useful (0 votes)
43 views3 pages

Digilabs Task (1-7)

The document contains examples of using various Python programming concepts like loops, functions, data types, conditionals, lists, dictionaries, strings and more. It shows how to define and call functions, access elements in lists and dictionaries, perform operations on strings, check conditions, iterate through lists and more.

Uploaded by

sambhvathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

Digilabs Task (1-7)

The document contains examples of using various Python programming concepts like loops, functions, data types, conditionals, lists, dictionaries, strings and more. It shows how to define and call functions, access elements in lists and dictionaries, perform operations on strings, check conditions, iterate through lists and more.

Uploaded by

sambhvathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

‭* Use a loop to print all prime numbers up to a given limit‬

‭def prime(num):‬
‭if num <= 1:‬
‭return False‬
‭for i in range(2, int(num**0.5) + 1):‬
‭if num % i == 0:‬
‭return False‬
‭return True‬

‭def print_limit(limit):‬
‭for num in range(2, limit + 1):‬
‭if prime(num):‬
‭print(num)‬

l‭imit = int(input("Enter the limit: "))‬


‭print("Prime numbers up to", limit, "are:")‬
‭print_limit(limit)‬

‭* Create an array of lists. * Access and print specific elements from the nested lists.‬

‭array = [‬
‭[1, 2, 3],‬
‭['a', 'b', 'c'],‬
‭[True, False, True]‬
‭]‬

‭ rint("Element at index [0][1]:", array[0][1])‬


p
‭print("Element at index [1][2]:", array[1][2])‬
‭print("Element at index [2][0]:", array[2][0])‬

*‭ Create a string and find its length. * Replace a specific substring in the string.‬
‭* Convert the string to uppercase and lowercase.‬

‭my_string = "Hello, world!"‬

l‭ength_of_string = len(my_string)‬
‭print("Length of the string:", length_of_string)‬

‭ ew_string = my_string.replace("world", "Python")‬


n
‭print("String after replacement:", new_string)‬

‭ ppercase_string = my_string.upper()‬
u
‭lowercase_string = my_string.lower()‬

‭ rint("Uppercase string:", uppercase_string)‬


p
‭print("Lowercase string:", lowercase_string)‬
*‭ Create a dictionary and initialize it with some items.‬
‭* Create an empty dictionary.‬
‭* Add key-value pairs to that empty dictionary at runtime.‬
‭* Create a nested dictionary.‬

‭ y_Dictionary = {'apple': 2, 'banana': 3, 'orange': 5}‬


m
‭print("Initialized Dictionaryionary:", my_Dictionary)‬

‭ mpty_Dictionary = {}‬
e
‭print("Empty Dictionaryionary:", empty_Dictionary)‬

‭ mpty_Dictionary['grape'] = 4‬
e
‭empty_Dictionary['kiwi'] = 6‬
‭print("Dictionaryionary after adding key-value pairs:", empty_Dictionary)‬

‭ ested_Dictionary = {'fruit': {'apple': 2, 'banana': 3}, 'vegetable': {'carrot': 4, 'spinach': 5}}‬


n
‭print("Nested Dictionaryionary:", nested_Dictionary)‬

*‭ Create a list that includes elements of different data types (integers, strings, floats, Boolean).‬
‭* Print the type of each element in the list.‬

‭my_list = [42, "hello", 3.14, True]‬

‭for element in my_list:‬


‭print("Type of", element, ":", type(element))‬

*‭ Create a list and initialize it with some items.‬


‭* Create an empty list and add some elements to it.‬
‭* Create an empty list and add elements to it dynamically‬
‭* Print the elements and their indices using a for loop.‬
‭* Find the length of the list.‬

l‭ist1 = [1, 2, 3, 4, 5]‬


‭print("Initialized list 1:", list1)‬

‭ mptylist = []‬
e
‭emptylist.extend([6, 7, 8, 9, 10])‬
‭print("List 1 after adding elements:", emptylist)‬

‭ mptylist2 = []‬
e
‭for i in range(1, 6):‬
‭emptylist2.append(i)‬
‭print("List 2 after adding elements dynamically:", emptylist2)‬

‭ rint("Elements and their indices in list 1:")‬


p
‭for index, value in enumerate(list1):‬
‭print("Index:", index, ", Value:", value)‬

l‭istlen = len(list1)‬
‭print("Length of list 1:", listlen)‬
‭ rite a Python program that checks if a person is eligible to vote based on their age.‬
W
‭The voting age greater than 18 years‬
‭Example Output: Enter your age: 20 You are eligible to vote.‬
‭Note: your program should prompts the user to input their age‬

‭age = int(input("Enter your age: "))‬

‭if age >= 18:‬


‭print("You are eligible to vote.")‬
‭else:‬
‭print("You are not eligible to vote.")‬

You might also like