Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Key takeaways

  1. The print() function sends data to the console, while the input() function gets data from console.
  2. The input() function comes with an optional parameter: the prompt string. It allows you to write a message before the user input, e.g.:
    name = input("Enter your name: ")
    print("Hello, " + name + ". Nice to meet you!")
  3. When the input() function is called, the program's flow is stopped, the prompt symbol keeps blinking (it prompts the user to take action when the console is switched to input mode) until the user has entered an input and/or pressed the Enter key. 

NOTE

You can test the functionality of the input() function in its full scope locally on your machine. For resource optimization reasons, we have limited the maximum program execution time in Edube to a few seconds. Go to the Sandbox, copy-paste the above snippet, run the program, and do nothing - just wait a few seconds to see what happens. Your program should be stopped automatically after a short moment. Now open IDLE, and run the same program there - can you see the difference? 

Tip: the above-mentioned feature of the input() function can be used to prompt the user to end a program. Look at the code below:

name = input("Enter your name: ")
print("Hello, " + name + ". Nice to meet you!")
print("\nPress Enter to end the program.")
input()
print("THE END.")

4. The result of th einput() function is a string. You can add strings to each other using the concatenation (+) operator. Check out this code:

num_1 = input("Enter the first number: ") # Enter 12
num_2 = input("Enter the second number: ") # Enter 21
print(num_1 + num_2) # the program returns 1221

5. You can also multiply (* - replication) strings, e.g.:

my_input = input("Enter something: ") # Example input: hello
print(my_input * 3) # Expected output: hellohellohello

Exercise 1

What is the output of the following snippet?

x = int(input("Enter a number: ")) # The user enters 2
print(x * "5")

55

If the user enters 2 as the input, the output of the snippet will be the string "55". Here's why:

  • The input() function is used to prompt the user for a number. In this case, the user enters 2.
  • The int() function is used to convert the user's input (which is initially a string) to an integer. So, x now has the value 2.
  • The expression x * "5" is evaluated. Since x is an integer and "5" is a string, this expression will repeat the string "5" x times. So, the output will be the string "55".

Exercise 2

What is the expected output of the following snippet?

x = input("Enter a number: ") # The user enters 2
print(type(x))

<class 'str'>

Here's why:

  • The input() function is used to prompt the user for a number. In this case, the user enters 2.
  • The value entered by the user is treated as a string and assigned to the variable x.
  • The type() function is then used to determine the data type of x. Since the value of x is a string (even though it represents a numerical value), the output will be <class 'str'>.

Congratulations! You have completed Module 2.

Well done! You've reached the end of Module 2 and completed a major milestone in your Python programming education. 

Here's a short summary of the objectives you've covered and got familiar in Module 2:

  • the basic methods of formatting and outputting data offered in Python, together with the primary kinds of data and numerical operators, their mutual relations and bindings
  • the concept of variables and variable naming conventions 
  • the assignment operator, the rules governing the building of expressions
  • the inputting and converting of data

You are now ready to take the module quiz and attempt the final challenge: Module 2 test, which will help gauge what you've learned so far. 

 

Key takeaways

David Khieu
Module by David Khieu, updated more than 1 year ago

Description

Key takeaways
No tags specified