Okay guys, as you all know, Python is one of the simplest programming languages to learn. This is your first chapter where you will actually write and run your first Python code. But before that, I want to tell you that every language has its own way of writing—just like English has its own grammar and Hindi has its own. Similarly, Python also has its own way of writing, which is called syntax. Technically, syntax refers to a set of rules or instructions that you need to follow when writing a program.
Table of Contents
Print “Hello World”
As we know, Python provides two different ways to execute code:
- Shell mode (executes code line by line and provides a command-line interface).
- Script mode (write your code in files, and Python executes it all at once).
You can use either of these modes to run your Python code.
# First program
print("Hello World")
PythonIn the above code, first, we have written print
, which is a function. To call a function, we need to use parentheses. Hence, print()
is the calling of the print
function. But you might not know what a function is. Functions are nothing but a set of instructions, and print
is one of the predefined (or built-in) functions provided by Python itself for direct use. This function is used to display content on the console (terminal) that is written inside it, either directly, using a variable, or in any other way.
As we know, Python has multiple data types. Here, we are using a string ("Hello World"
). However, we can use any other valid Python data type to print.
Print multiple data
The above code prints "Hello World"
on the console. But what if you want to print a string and an integer simultaneously? The print()
function can print any number of values that you want. Just separate them using a ,
(comma).
# Print multiple values
print("Name:", "Inkita\n", "Age:", 22)
PythonThe above code produces the following output:
Name: Inkita
Age: 22
BashSeparate multiple values
As you can see, after "Name:"
, a space is used to separate it from "Inkita"
. The "\n"
then changes the line. Similarly, "Age: 22"
follows. But wait—how does this space appear? Actually, Python provides an argument to the print()
function called sep
, which has a default value of a single white space (). You can change it as shown below:
# Modify separator in print
print("Name:", "Inkita\n", "Age:", 22, sep="#")
PythonHere’s the output:
Name:#Inkita#Age:#22
BashBoom! Now the space is replaced with the string "#"
. Similarly, the print()
function provides another argument called end
, which determines what should be used at the end after printing everything. By default, it is "\n"
(newline). This is why, when you use two different print()
statements, they each end with a new line.
print("SHRAY")
print("SALVI")
PythonOutput:
SHRAY
SALVI
BashYou can change this behavior as well.
# Changing the end character of print
print("SHRAY", end="-")
print("SALVI")
PythonOutput:
SHRAY-SALVI
BashComments
Throughout this conversation and the examples, did you notice that I wrote lines starting with #
that didn’t do anything? Those are comments. Python provides two different ways to add comments in a program. Comments don’t affect the program during runtime; instead, they are used to explain what you have written and why.
- Single-line comment: It can be written by putting a
#
at the beginning, followed by your comment. - Multi-line comment: It is essentially a multi-line string that does nothing if it is not assigned to a variable.
# This is a single-line comment
"""
This is a multi-line comment.
It spans multiple lines.
"""
PythonConclusion
So you have learned how to write and execute your first Python program using the print()
function, understood how the print()
function can display different types of data on the console, and how to customize the behavior of the print()
function using the sep
and end
arguments. The importance of comments, which help explain your code without affecting its execution. So welcome to the journey of learning Python. Be consistent and have a never-give-up attitude.