Python Programming Exercises for Beginners
Python Programming Exercises for Beginners
To calculate the area of a circle given its radius in Python, import the constant pi from the math module and use the formula for the area of a circle (A = πr²). Define a function `CircleArea` that takes the radius as an argument, calculates the area using `area = pi * r ** 2`, and returns the result .
To switch the positions of the first and last elements in a list in Python, use indexing to access and swap their positions. For a list `color_list`, this can be achieved by storing the first element in a temp variable, assigning the last element to the first position, and then assigning the temp variable to the last position: `temp = color_list[0]; color_list[0] = color_list[-1]; color_list[-1] = temp` .
To count occurrences of the integer 4 in a list using Python, define a function `list_count` that iterates through the list, increments a counter each time 4 is encountered, and returns the count. For example: `def list_count(numbers): count = 0; for num in numbers: if num == 4: count += 1; return count` .
To read specific lines from a CSV file and extract them into a list using Python, open the file, iterate over its lines till reaching the desired line, and split the line into a list based on commas. For example, to extract the second line: `filename = open('data.csv', 'r'); for line in range(0,2): Line = filename.readline(); loaded_data = line.split(',')` .
To determine if a number is even or odd in Python, use the modulus operator `%` to check the remainder when dividing by 2. If `number % 2` equals 0, the number is even; otherwise, it is odd. Implement a conditional check: `mod = number % 2; if mod > 0: print('This is an odd number.'); else: print('This is an even number.')` .
To compute the number of days between two specific dates in Python, use the `date` class from the `datetime` module, instantiate two date objects, and find the difference between them using subtraction which yields a timedelta object. For example: `f_date = date(2023, 10, 10); l_date = date(2023, 11, 18); delta = l_date - f_date; print(delta.days)` .
To compute the value of an integer expressed as n+nn+nnn, convert the integer into a string and repeatedly concatenate it to form 'nn' and 'nnn'. Then convert these strings back to integers and sum them: `n1 = int('%s' % a); n2 = int('%s%s' % (a, a)); n3 = int('%s%s%s' % (a, a, a)); n = n1 + n2 + n3`. Alternatively, use simple arithmetic with multiplication: `n = a*111 + a*11 + a` .
To store a user's full name in a text file using Python, open the file in append mode and write the name to it. Define a function that takes the last name and first name as inputs, opens 'users.txt' in append mode, concatenates the names with a space and newline, then writes it to the file: `filename = open('users.txt', 'a'); fullname = firstname + ' ' + lastname + '\n'; filename.write(fullname)` .
To generate both a list and a tuple from a sequence of comma-separated numbers in Python, use the string `split` method to break the sequence into a list, and then convert this list into a tuple. Assuming `numbers` is a string of numbers, apply `numbers_list = numbers.split(',')` to get a list. Convert to a tuple with `numbers_tuple = tuple(numbers_list)` .
To display the current date and time in Python, use the `datetime` module. First, import it, then call `now()` method of `datetime` class to get the current date and time: `import datetime; now = datetime.datetime.now(); print('Current date and time : ', now)` .