Python Print Function and Syntax
Now, you are all set to start your Python coding journey. Before beginning to code
let us learn about the rules of writing any program called syntax and print function.
Programming Syntax: The term programming syntax refers to the set of rules that
dictates how programs written in a programming language should be structured.
It encompasses the correct arrangement of symbols, keywords, and other
elements to form valid and understandable code. Syntax is crucial in
programming because even a small deviation from the prescribed rules can lead
to errors and affect the program's functionality. Each programming language has
its own syntax, and adhering to these rules is essential for creating code that the
computer can interpret and execute accurately.
Let us make our first Python program and learn to make other program in Python.
Are you ready?
Python Print Function
The print() function prints the specified message to the screen, or to any other
standard output device. The standard output device is often the computer screen
or console, and when a program generates output, it is directed to this default
output device unless specified otherwise in the code. See the examples to know
how it is written.
Note: Python supports the single quote and the double quote for writing these
messages.
If you begin a message with a single quote, you must end it with a single quote.
The same goes for double-quotes.
Run a Program: After writing the message in print function within a Code cell we
need to either press Run button or use Shortcut keys Shift+Enter. The output gets
displayed after every cell. Running a program involves the interpreter reading the
source code line by line and performing the corresponding actions.
1|Page
Here are some examples of simple messages on screen using print() function.
Alt text: Code showing printing messages to the console using print() function.
Why do you think the last print function is showing an error?
Because we started the message with a single quote but ended it with a double
quote.
Writing such a code will show Syntax error which means we have broken the rule
of writing a programming language.
In the examples, the first two code cells contain valid Python statements. In
Python, a statement is a single line of code that serves as a complete instruction,
capable of performing a specific action. These statements are executable by the
Python interpreter.
Multiple Statements in One Line: For writing multiple statements in one line, we
can separate them with a semicolon. Here is an example:
Alt text: Multiple statements in one line in Python code.
Note: When you write one or more such Python statements then it is called as
Python Script.
2|Page
In the process of developing extensive programs having multiple Python
statements, programmers often want to write something about their code to
enhance self-understanding and facilitate collaboration with other developers.
This can be done using Comments. Let us learn how we can give comments in a
Python program.
Multi-line statement: When Python encounters a ‘backslash’ at the end of a line, it
treats it as a line continuation, allowing the string to span multiple lines. In this
case, it's equivalent to writing the string on a single line. Here is an example:
Alt text: Multi-line statements using backslash.
3|Page