3️⃣

1.3 Input/Output

Output

To output a value, use the print function. When you print something, it will appear in the output viewer (aka "the console"). You can print variables, strings, integers, floats, and all data types. We'll get to data types soon.

print("Hello")
print(1)
print(99.99)

View code on GitHub.

The code above will output:

Hello
1
99.99

Now, there are also other ways to use the print function, including:

# strings can be in single or double quotes
print("Message")
print('Message')

# You can print multiple arguments; you separate arguments with a comma
# When the arguments are printed, they are separated by a space by default
print('Message', 'with', 'arguments') # would have spaces

# You can even use string concatenation ("adding" strings together)
# String concatenation puts strings together w/o spaces (be careful about this)
print('Message' + 'with' + 'concatenation')  # would not have spaces

View code on GitHub.

The output of that:

Message
Message
Message with arguments
Messagewithconcatenation

Input

To input a value, use the input() function. The input() function gets user input from the keyboard. When using it, make sure to assign the input() to a variable that way you store it and can print it.

It is also worth noting that the input prompt goes inside the input function's parentheses. For example, if you wanted to provide the prompt: "How are you? ", you'd do:

status = input("How are you?  ")

Example:

print("Hello World")
name = input("What is your name?  ")
print(name) # prints the name that the user entered

View code on GitHub.

If you're confused about variables, click this.

Previous Section

2️⃣
1.2 Comments

Next Section

4️⃣
1.4 Errors

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.