Python uses print() to print output text or values to the console.
print() in Python inserts a new line at the end of the output
and insert a whitespace to separate each element automatically.
Syntax:
print("Some random text") print(variable_name1, variable_name2)
In Python, when we want to print int type of variable together with
string, we have to type cast it into str first,
so that it can be printed. Another way is to use the f
operator and format function.
print("Some text has " + str(other_variable) + " length") print(f"Some text has {other_variable} length") print("Hello {} {}, hope you're well!".format(variable1, variable2))
If we don't want Python to automatically insert a new line after each line printed
or insert a white space to separate the variables, we can specify it using
end="...", sep="..." in the print statement.
If not, then Python will automatically set
end="\n" and sep=" ".
In Python, the double quote and the single quote can both be used to define
string. \n is an escape sequence used to insert a new line.
Syntax: print(variable_name1, variable_name2, end=" ", sep="")
Example:
myAge = 18 yourAge = 20 print("Hello World", "bye") print('Hello World', 'bye', end="", sep="\n") print("\nI am " + str(myAge) + " years old") print(f"You are {yourAge} years old") print("The ages are {} and {}.".format(myAge, yourAge))
Output:
Hello World bye Hello World bye I am 18 years old You are 20 years old The ages are 18 and 20.
In Python, you can use the input function to get user input. Note that if we don't typecast the input, then it will automatically be treated as a string by Python.
Syntax: variable_name = input("Enter a string: ")
Example:
#input is treated as string myNum = input("Enter a number: ") #input is treated as int myNum2 = int(input("Enter a number: "))
Comments can be used to explain what does a block of code do. Comments can also be used to prevent the execution of some lines of code. Python comment is a single line.
Syntax: #some comment here
Any text between # and the end of the line will not be executed by the compiler.
Example:
#this line will not be executed myNum = input("Enter a number: ") print(myNum) #print("hello") #"hello" will not be printed out because #it is in the comment block.
Escape sequences are mostly used to control or format the output.
It started with a backslash (\) and followed by another character.
Example:
print("Hello,\nWorld!"); #The output will be #Hello, #World! #Instead of #Hello,World!
Below are the most commonly used escape sequences.
| Escape Sequences | Description |
|---|---|
| \n (newline) | Insert a new line on the output. |
| \t (tab) | Insert a horizontal tab on the output. |
| \\ (backslash) | Backslash can't be printed directly as it
is used for escape sequences in Python so we have to place another backslash following the backslash used for an escape sequence. So, to print backslash, we type \\ not \ |
| \' (single quote) | Print a single quote in the output |
| \" (double quote) | Print a double quote in the output |
| \? (question mark) | Print a question mark in the output |
| \b (backspace) | Move the cursor one position back in the current line of text. |
| \a (alert or bell) | A beep is generated by the computer on execution. |
| \r (carriage return) | Move the cursor to the beginning of the current line. |
| \v (vertical tab) | Insert a vertical tab on the output. |
An exception is an exceptional circumstance that appears when a program is executed.
For example, division by zero, invalid inputs, and many more. Python exception handling
consists of four major keywords with else
and finally being optional:
trytry block identifies which block of code
will be tested and performed exception-catching.
The keyword try has to be followed by one or
more except keywords to perform the exception-catching.
exceptexcept keyword indicates the catching of an exception.
elseelse keyword lets you execute code when there is no error.
finallyfinally keyword lets you execute code regardless of the result of the exception-catching.
Syntax:
try: #code block except error_name: #code block except: #code block else: #code block finally: #code block
Example:
try: print(x) except NameError: print("Variable x is not defined") except: print("Something else went wrong") else: print("Nothing went wrong") finally: print("Exception handling process is done")
Output:
Variable x is not defined Exception handling process is done
In this case, the variable x is not defined, so there is
NameError. Therefore, the message
Variable x is not defined is printed. If the error is not
NameError, then the message Something else went wrong
will be printed and if there is no error at all, then the message
Nothing went wrong will be printed. The message in
finally will always be printed regardless the result.