Python Coding Practice

0 of 23 passed

Continue practice

Fundamentals

  • Add a Delivery ChargeFundamentals Write calculate_order_total(price_text, delivery_charge). Convert the valid numeric price text to a number, add the delivery charge, and return the total.
  • Check Free DeliveryFundamentals Write is_free_delivery(order_total, is_member). Return True when the order total is at least Rs. 75 or the customer is a member; otherwise return False.
  • Normalize a UsernameFundamentals Write normalize_username(username). Remove whitespace from both ends and return the username in lowercase. Keep internal spaces and punctuation unchanged.
  • Format a Purchase MessageFundamentals Write format_purchase_message(name, item, price). Return a sentence in this exact format: Name bought a item for Rs. 10.56. Show price with exactly two digits after the decimal point.

Data Structures

  • Find the Top-Scoring SubjectData Structures Write top_scoring_subject(subjects, scores). The lists have the same non-zero length. Return the subject at the highest score; if scores tie, return the first matching subject.
  • Average a List of MarksData Structures Write average_marks(marks). The list contains at least one numeric mark. Return the arithmetic average without rounding it.
  • Find the Highest-Paid EmployeeData Structures Write highest_paid_employee(names, salaries). The lists have the same non-zero length, and matching indexes belong to the same employee. Return [name, salary] for the highest-paid employee; use the first employee when salaries tie.
  • List Unique Shop ItemsData Structures Write sorted_unique_items(items). The input contains lowercase item names. Return each distinct name once, sorted in ascending alphabetical order.
  • Read a Student GradeData Structures Write read_student_grade(student, default_grade). Return the value for the grade key when it exists; otherwise return default_grade.

Control Flow

  • Set an Event Ticket PriceControl Flow Write get_ticket_price(age, is_student). Ages under 5 are free; ages 5 through 17 cost Rs. 6; adults who are students pay Rs. 8; other adults pay Rs. 12. Age is a non-negative integer.
  • Sum Two NumbersControl Flow Write a Python function named sum_numbers(a, b) that returns the sum of two numbers. Check your function with positive, negative, and mixed values.
  • Fahrenheit to CelsiusControl Flow Write a Python function named to_celsius(fahrenheit) that converts Fahrenheit to Celsius using (F - 32) / 1.8 and returns the result. Try freezing, room, and boiling temperatures.
  • Greet a StudentControl Flow Write greet_student(name, course='Python'). Return 'Hello, {name}, Welcome to {course} class'. Use Python's default argument when no course is provided.
  • Count Passing ScoresControl Flow Write count_passing_scores(scores, pass_mark). Return how many scores are greater than or equal to pass_mark. Return 0 for an empty list.
  • Summarize Shop OrdersControl Flow Write summarize_orders(orders). Each order has quantity and unit_price. Return a dictionary with total_units and revenue; an empty list returns zero for both values.
  • Filter Words by LengthControl Flow Write filter_words_by_length(words, minimum_length). Use filter() to return words whose length is at least minimum_length, keeping their original order.

File, Error, OOP

  • Convert Phone Records to RowsFile, Error, OOP Write phone_records_to_rows(contacts). Each dictionary key is a phone number and each value has name, age, and course. Return a list of records with those fields plus phone, preserving input order.
  • Handle Invalid Density InputsFile, Error, OOP Write safe_density(mass, volume). Return mass divided by volume; use try/except to return None when the input types cannot be divided or volume is zero.
  • Build a Car ClassFile, Error, OOP Define Car with brand, model, and a color that defaults to blue. Its display_info(), start_engine(), and stop_engine() methods return the specified car details. car_messages(brand, model, color='blue') must return those three messages in that order.
  • Override Animal SoundsFile, Error, OOP Define Animal.speak() to return 'Animal speaks'. Dog and Cat must inherit from Animal and override speak() to return 'Dog barks' and 'Cat meows'. make_animal_sounds(types) returns each sound in input order; accepted types are animal, dog, and cat.

Library, SQL, Git

  • Find HCF and LCMLibrary, SQL, Git Write hcf_and_lcm(numbers) for a non-empty list of positive integers. Return a two-item list containing the HCF first and LCM second, using Python's math library.
  • Find Days Between DatesLibrary, SQL, Git Write days_between_dates(start_date, end_date). Both strings use YYYY-MM-DD. Return the signed number of days from start_date to end_date; an earlier end date gives a negative result.
  • Calculate Boxes NeededLibrary, SQL, Git Write boxes_needed(item_count, box_capacity). Return the number of boxes needed when items are packed into boxes of the given positive capacity. Zero items need zero boxes.