Python: Intro – Basic Questions

*Please note that the comments in the code, started by the hashtag, are for educational purposes and do not change the code.

Question 1: Introduction: Hello, World!
Printing text (e.g. Hello World)
Task: Output the string ‘Hello, World!’

Answer:

Explanation: Quotations are required because we want to print a string. However, if the task required us to assign a variable to the string (for example: string = ‘Hello, World!’), then it is also possible to print the string we wanted by typing print(string).

Question 2: Input and Output
Storing of data (such as strings) in variables. In this question we use variables to store & combine strings
Task: Add two strings together
‘reap’, ‘pear’ -> ‘reappear’
‘Cat’, ‘Dog’ -> ‘CatDog’
‘a’, ” -> ‘a’

Answer:

Explanation: When this is run in Python, it will ask for the first word, and the second word. Then it will print and combine the two strings together. This is done by assigning a variable (combined) to combine the two strings that have a variable assigned to each of them (word1 and word2).

Question 3: Converting Input Strings
If we have a string which represents a number (say, ’60’), we can convert it into an integer using the int function. In other words, int(’60’) will give us the number 60.
Task: Print the number of minutes in an hour
‘0’ -> ‘0’
‘2’ -> ‘120’

Answer:

Explanation: What this code first does is assign a variable to the string we put in. After this, we make this string an integer and assign it to another variable. From this, we can create another variable that will multiply the integer we have by 60 (because there are 60 minutes in an hour). After this, the result is printed.

, , , , , , , , ,

No comments yet.

Leave a Reply

Powered by WordPress. Designed by WooThemes

Free WordPress Themes