Basic IO
Output with variables
Variables can be used as output, like anything else using print().
You need to incase the variable with f-strings, by adding a letter and {}.
Here is an example:
age = 10
print(f"His age is: {age}")Input
Values are typically recieved from an outer source, like a user.
The following is written when getting values from a user or system:
var = input() # var will hold the value of the string.Cast
To convert the input into a different type, cast is used.
As an example, to cast a string into an int, aka. a whole number, the following is written:
var = input()
var = int(var)
# Can be simplified into
var = int(input())If input is a number, then var will hold a number. If input contains a invalid number, then program will fail.
Cast | Explanation |
int() | Convert into a whole number. |
float() | Convert into a real number. |
bool() | Convert into a boolean. |
str() | Convert into a string. |