Practice problems

 

 

#1: About Variables and Strings:

References: Chapter 1 (Sections 1.1 – 1.4) and Chapter 2 of the book Think Python in HTML

 

Write a Python program to ask for a positive real number and as the radius of a circle and then determine the area of a circle given its radius.

a)     Print a message to ask the user to enter the radius of the circle.

b)    Use the raw_input function to read the radius of the circle as a string and assign it into a variable CString

c)     Use the float function to convert the string in CString into a real number. Assign the real number into another variable radius.

d)    Calculate the area of a circle based on the value stored in radius and assign the result into another variable area. In other words, area = 3.14* radius* radius.

e)     Print a message on the screen to inform the user that the area of the circle.

 

 

#2: About Basic Math, Decision Control and Conditional Statements:

References: Read Sections 5.1 ~ 5.7 and Section 5.11 of the book Think Python in HTML.

 

Write a Python program to ask for a positive integer, print out the remainders of the integer divided by 4, by 100, and by 400 respectively, and then print out messages saying whether the given positive integer is divisible by 4, by 100, and by 400 respectively.

a)     Print a message to ask the user to enter a positive integer.

b)    Use the raw_input function to read the integer as a string and assign it into a variable CString

c)     Use the int function to convert the string in CString into the integer value. Assign the integer value into another variable n.

d)    Use the modulo operator % operator to determine and print out the remainder of n divided by 4, n divided by 100, n divided by 400, respectively.

e)     Use an if-else statement to check whether the remainder of n divided by 4 is 0. If so print out a message saying that it is divisible by 4; otherwise print a message to say it is not. Similarly use if-else statements n to determine and say whether the given positive integer is divisible by 100 and by 400 respectively.

 

 

 

#3: About Decision Control and Conditional Statements:

References: Read Sections 5.1 ~ 5.7 and Section 5.11 of the book Think Python in HTML.

 

Leap years:

A year is a leap year if it is divisible by 4 except that any year divisible by 100 is a leap year only if it is also divisible by 400. So 1900 is not a leap year, but 2000 is. In other words,

 

Write a Python program to ask for a positive integer, determine whether it is a leap year, and then print out a message to inform the user whether it is leap year.

a)     Follow the steps for problem #2 above to ask for a positive integer and determine whether the given positive integer is divisible by 4, by 100, and by 400 respectively. Then use an if-else statement to determine whether the integer represents a leap year based on the definition of leap years above.

 

 

#4: About Loops:

References: Read Sections 7.1 ~ 7.4 of the book Think Python in HTML

 

Print out all the even numbers between two given numbers: Write a Python program to ask for two positive integers represented, print out all the even numbers between the two given positive integers.

 

 

#5: About Loops:

References: Read Sections 7.1 ~ 7.4 of the book Think Python in HTML

 

Print out all the leap years between two given years: Write a Python program to ask for two positive integers represented two years, print out all the leap years between the two given years.

 

 

#6: About the simple nim game TLCW: See a possible solution to the TLCW game for Python Programming #3B in the solution file.

References: Read Sections 7.1 ~ 7.4 of the book Think Python in HTML