Exercises
Exercise python for beginners on Numbers, Variables and Strings.
- The Greeter
Write a program that asks for the user’s name tells them hello. - What’s My Age Again?
Ask for their year of birth and compute their age.
Print out their age in the following format:
Your age is 25. - Ratio
Read two numbers in (user input).
Print out their ratio.
Figure out how to make this program fail. (There are many ways.) - Sum and Mean
Read five numbers in.
Print out their sum and mean. - Swap
The user enters two numbers.
Store the numbers in two variables called input1 and input2.
Swap the values of the two variables so that input1 has the value of input2
and vice versa.
Print out the two variables. - Quickswap
Perform the swap in #5 above in only 1 line of code, and without using any
variables other than input1 and input2.
More exercise
- Create a string containing a single quote.
- Create a string containing a double quote.
- Create a string containing both a single quote a double quote
10.Create a triple quoted string that contains single and double quotes.
11.Create a character “a”.
12.Create a character, then obtain its integer representation.
13.Given these strings: - >>> s1 = ‘abcd’
- >>> s2 = ‘efgh’
Create a new string composed of the first string followed by (concatenated with)
the second.
16.Create a single string containing 5 copies of the string ‘abc’.
17.Use the multiplication operator to create a “line” of 50 dashes.
18.Strip all the whitespace characters off the right end of a string.
19.Center a short string within a longer string, that is, pad a short string with blank
characters on both right and left to center it.
20.Convert a string to all upper case.
21.Split a string into a list of “words”.
22.Create a string that contains a backslash character using both plain literal string
and a raw string
0 Comments