Introduction to Python — Session 2
In this session we cover:
- Commenting your code
- Casting integers, floats and strings
- String indexes
- Length of strings
- Requesting user input
- Changing the case of letters
Write your code
Questions
Section A
- Create two variables that hold the width and height of a rectangle, work out and store the area in a third variable. Print out the string:
Rectangle of width *x* and height *y* has an area of *area*
. - Write code that prints the length of the string, 'python'.
- Print out the first and third letter of the word 'python'.
- Ask the user to enter their name, and print out
Hello, *name*
. - Ask the user to enter their age, tell them how old they will be in 15 years time.
- Combine the two input statements above and print out the message
Hello, *name*, you are currently *age* years old. In 15 years time you will be *age_in_15_years_time*
. - Ask the user to enter their hometown, print it out in uppercase letters.
- Ask the user to enter their favourite colour and find out the length of the colour they input.
- Ask the user to enter the weather and the month. Print out the string,
It is *month* and it is *weather* today
. - Ask the user to enter 5 different temperatures and the month. Work out the average temperature and print out the string,
It is *month* and the average temperature is *average_temperature* degrees celsius
. - Print out the above sentence but make the month upper case.
- Create a variable that holds your favourite animals and print it out. Make sure the animals are all on different lines and tabbed.
- Ask the user to enter their name as well as a number. Print out the uppercase character at that position in the string.