C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: Print can handle variables. We show a string variable, but other values (like integers) and even objects can be printed.
Python program that uses print statement
# Print a string literal.
print("Hello")
# Print two arguments.
print("Hello", "there")
# Print the value of a string variable.
a = "Python"
print(a)
Output
Hello
Hello there
Python
Then: The user types some characters, such as "x" and presses enter. The input() method immediately returns this value.
Loop: Often a loop is needed when designing console programs. This makes the program interactive.
Python program that uses input
# Get input from console.
s = input()
print("You typed", s)
# Use if-statement on input.
if s == "a":
print("Letter a detected")
else:
print("Not letter a")
Output
a
You typed a
Letter a detected
Here: We use a while-true loop and break if the user types a lowercase "q". The loop continues infinitely until a "q" is received.
While True, BreakPython program that interacts with user
# Continue while true.
while True:
# Get input.
value = input()
# Break if user types q.
if value == "q":
break
# Display value.
print("You typed: ", value)
# Exit message.
print("You quit.")
Output
1
You typed: 1
2
You typed: 2
3
You typed: 3
q
You quit.
Tip: When creating a program, it is best to use the data type that most closely matches your data.
Python program that converts console input
print("Enter number:")
# Get input and convert it to an integer.
value = input()
number1 = int(value)
print("Again:")
value = input()
number2 = int(value)
print("Product:")
# Multiply the two numbers.
print(number1 * number2)
Output
Enter number:
10
Again:
2
Product:
20
So: In this example we continually prompt the user (by passing a string argument to the input method).
And: We convert the string received into an integer. If the value is not numeric, int() will raise an error. The loop will continue.
Try, RaisePython program that validates input
while True:
try:
# Get input with prompt.
code = input("Code: ")
# Attempt to parse input.
value = int(code)
break;
except:
print("Invalid code")
print("Value:", value)
Output
Code: g
Invalid code
Code: 89
Value: 89
Argument: This can be any term that Python can evaluate. Here I try the keyword "dict" which constructs a dictionary.
Example help command use: Python
>>>> help(dict)
Help on class dict in module builtins:
class dict(object)
| dict() -> new empty dictionary.
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs.
Open: We can specify a file object. Often programs receive this object from the open() method.
FileNote: When using this syntax, each call to print must specify the file argument. Otherwise, the default output (the console) is used.
Note 2: In most programs, using the file object directly leads to clearer code.
Python program that uses print, file argument
# Open this file for writing.
f = open("C:\\profiles\\Codex.txt", "w")
# Print lines to the file.
print("Some text", file=f)
print("Some more text", file=f)
File contents: Codex.txt
Some text
Some more text
Here: In this example, we set end to an empty string literal. So print renders no end.
Tip: By setting end to an empty string, we can use print multiple times on a single line.
Tip 2: We can avoid concatenating strings before displaying them in this way. Please also consider calling print() with multiple arguments.
Python program that uses print, end
# Change end argument to avoid newline.
print("Hi, ", end="")
print("how are you?")
Output
Hi, how are you?
Here: The output contains a blank line (with zero length) between the two numbers 100 and 200.
Python program that uses print, no arguments
print(100)
# Use an empty print statement.
print()
print(200)
Output
100
200
Info: To override this behavior, you need to construct a string out of your numbers before passing anything to print.
Convert Int, StringPython program that uses expressions
# This prints 3.
print(1 + 2)
# This prints the expression.
print("1 + 2")
Output
3
1 + 2
Python program that uses format, print
# Print formatted string.
value = 10
print(str.format("There are {} apples", value))
Output
There are 10 apples
So: In Python 3 programs, use those parentheses. I leave it to smarter people than me to design these languages.