Advertisement
plirof2

pythonbasics.org/exercises/

May 9th, 2023
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.11 KB | None | 0 0
  1. https://pythonbasics.org/exercises/
  2. Python Practice
  3.  
  4. SOLUTIONS: https://pythonbasics.org/pythonbasics-exercise-answers.pdf
  5.  
  6. Beginner exercises
  7. Run Python programs
  8.     • Make a Python program that prints your name.
  9.     • Make a program that displays the lyrics of a song.
  10. Variables
  11.     • Make a program that displays several numbers.
  12.     • Make a program that solves and shows the summation of 64 + 32.
  13.     • Do the same as in 2, but make it sum x + y.
  14. Strings
  15.     • Make a program that displays your favourite actor/actress.
  16.     • Try to print the word ‘lucky’ inside s.
  17.     • Try to print the day, month, year in the form “Today is 2/2/2016”.
  18. String replace
  19.     • Try the replace program
  20.     • Can a string be replaced twice?
  21.     • Does replace only work with words or also phrases?
  22. String find
  23.     • Find out if string find is case sensitive
  24.     • What if a query string appers twice in the string?
  25.     • Write a program that asks console input and searches for a query.
  26. String join
  27.     • Create a list of words and join them, like the example above.
  28.     • Try changing the seperator string from a space to an underscore.
  29. String split
  30.     • Can a string be split on multiple characters?
  31.     • Can you split a string this string?: World,Earth,America,Canada
  32.     • Given an article, can you split it based on phrases?
  33. Random numbers
  34.     • Make a program that creates a random number and stores it into x.
  35.     • Make a program that prints 3 random numbers.
  36.     • Create a program that generates 100 random numbers and find the frequency of each number.
  37. Keyboard input
  38.     • Make a program that asks a phone number.
  39.     • Make a program that asks the users preferred programming language.
  40. If statements
  41.     • Make a program that asks the number between 1 and 10. If the number is out of range the program should display “invalid number”.
  42.     • Make a program that asks a password.
  43. For loops
  44.     1. Make a program that lists the countries in the set
  45. clist = ['Canada','USA','Mexico','Australia']
  46.     2. Create a loop that counts from 0 to 100
  47.     3. Make a multiplication table using a loop
  48.     4. Output the numbers 1 to 10 backwards using a loop
  49.     5. Create a loop that counts all even numbers to 10
  50.     6. Create a loop that sums the numbers from 100 to 200
  51. While loops
  52.     1. Make a program that lists the countries in the set below using a while loop.
  53. clist = ["Canada","USA","Mexico"]
  54.     2. What’s the difference between a while loop and a for loop?
  55.     3. Can you sum numbers in a while loop?
  56.     4. Can a for loop be used inside a while loop?
  57. Functions
  58.     1. Make a function that sums the list mylist = [1,2,3,4,5]
  59.     2. Can functions be called inside a function?
  60.     3. Can a function call itself? (hint: recursion)
  61.     4. Can variables defined in a function be used in another function? (hint: scope)
  62. Lists
  63.     • Make a program that displays the states in the U.S.
  64. states = [ 'Alabama', .. ,'Wyoming' ]
  65.     • Display all states starting with the letter M
  66. List operations
  67.     • Given the list y = [6,4,2] add the items 12, 8 and 4.
  68.     • Change the 2nd item of the list to 3.
  69. Sorting list
  70.     • Given a list with pairs, sort on the first element
  71. x = [ (3,6),(4,7),(5,9),(8,4),(3,1)]
  72.     • Now sort on the second element
  73. Range function
  74.     • Create a list of one thousand numbers
  75.     • Get the largest and smallest number from that list
  76.     • Create two lists, an even and odd one.
  77. Dictionary
  78.     • Make a mapping from countries to country short codes
  79.     • Print each item (key and value)
  80. Read file
  81.     • Read a file and number every line
  82.     • Find out what the program does if the file doesn’t exist.
  83.     • What happens if you create a file with another user and try to open it?
  84. Write file
  85.     • Write the text “Take it easy” to a file
  86.     • Write the line open(“text.txt) to a file
  87. Nested loops
  88.     • Given a tic-tac-toe board of 3x3, print every position
  89.     • Create a program where every person meets the other
  90. persons = [ “John”, “Marissa”, “Pete”, “Dayton” ]
  91.     • If a normal for loop finishes in n steps O(n), how many steps has a nested loop?
  92. Slices
  93.     • Take a slice of the list below:
  94. pizzas = [“Hawai”,”Pepperoni”,”Fromaggi”,”Napolitana”,”Diavoli”]
  95.     • Given the text “Hello World”, take the slice “World”
  96. Multiple return
  97.     • Create a function that returns a,b and a+b
  98.     • Create a function that returns 5 variables
  99. Scope
  100.     • Add a function reduce amount that changes the variable balance
  101.     • Create a function with a local variable
  102. Time and date
  103.     • Print the date in format year-month-day
  104. Try except
  105.     • Can try-except be used to catch invalid keyboard input?
  106.     • Can try-except catch the error if a file can’t be opened?
  107.     • When would you not use try-except?
  108. OOP exercises
  109. Class
  110.     • Can you have more than one class in a file?
  111.     • Can multiple objects be created from the same class?
  112.     • Can objects create classes?
  113.     • Using the code above, create another object
  114.     • Add a method to the class: location()
  115. Getter and setter
  116.     • Add a variable age and create a getter and setter
  117.     • Why would you use getter and setter methods?
  118. Modules
  119.     • Import the math module and call the sin function
  120.     • Create your own module with the function snake()
  121. Inheritance
  122.     • Create a new class that inherits from the class App
  123.     • Try to create a class that inherits from two super classes (multiple inheritance)
  124. Static method
  125.     • Can a method inside a class be called without creating an object?
  126.     • Why does not everybody like static methods?
  127. Iterable
  128.     • What is an iterable?
  129.     • Which types of data can be used with an iterable?
  130. Classmethod
  131.     • What is a classmethod?
  132.     • How does a classmethod differ from a staticmethod?
  133. Multiple inheritance
  134.     • Do all programming languages support multiple inheritance?
  135.     • Why would you not use multiple inheritance?
  136.     • Is there a limit to the number of classes you can inherit from?
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement