Python Practice
Python Practice
import math
def find_circle_circumference(radius):
""" Calculate the circumference of a circle given its
radius. """
circumference = 2 * math.pi * radius
return circumference
2. (5 Points) Write a function print_char(s) that takes one parameter: a string s. This
function should print out each character in s unless the character is a whitespace. Include
a docstring for the function.
def print_char(s):
""" Prints each character in the given string s, except
whitespace characters. """
for char in s:
if char != " ":
print(char)
1
3. (5 Points) Write a function print_students() that takes two parameters: a
dictionary grades where the keys are student names (strings) and the values are their
grades (integers), and a character char. The function should print each student's name
and grade in the format name:grade (without spaces) if the student's name starts with
char. Include a docstring for the function.
4. (10 Points)
a. Create a new class Student for students in a classroom. It has two attributes:
name (string) and grade (int). Then, create a constructor method
__init__() for the class, which should initiate the attribute name to
"unknown", and the attribute grade to 0.
b. Create a new object of the class and assign it to new_student
c. Change the value of attribute name of new_student to "Alice", and the
value of attribute grade to 80.
d. Create a new method add_bonus(bonus)for the class, which increases the
value of attribute grade by bonus.
e. Call the method add_bonus(bonus) on new_student to increase its
attribute grade by 5.
class Student:
""" A class representing a student with a name and
grade. """
def __init__(self):
self.name = "unknown"
self.grade = 0
2
self.grade += bonus
new_student = Student()
new_student.name = "Alice"
new_student.grade = 80
new_student.add_bonus(5)