Python is known for its simplicity. So, let's tell the computer to say hello to us. (We are assuming that you have gone through the Introduction chapter first and know how to write and compile Python programs. If not, then first go through the Introduction chapter.)
And that's it!
To print any message on screen, just type
print() and that message within single quotes
' ' or double quotes
" " inside
print(). Isn't it simple?
print() is a function which prints something on screen. (You will learn about functions in later sections)
In the above example, we printed the message by enclosing it within double quotes
" " i.e.,
"Hello World". Let’s print the same message by enclosing it within single quotes
We got the same output on printing "Hello World" and 'Hello World'.
Let's see some more examples:
print("Hey! I am an upcoming coder") print("I am going to step into world of programming")
print("I am loving this. Print, print and print!")
x = 10 y = 5 print("sum of", x, "and", y, "is", x+y)
You might have guessed what is happening here, yet let me explain a bit.
x = 10 → We are taking a variable
x and giving it a value of 10. Yeah, similar to what we do in Maths.
y = 5 → Similar to
x = 10,
y is a variable which is assigned a value of 5.
print("sum of",x,"and",y,"is",x+y) → We already know that the
print() function prints something for us but what's inside it is interesting. Let’s have a look at it.
Take a note that
"is" are written inside
" " (we can also use
' ') but
x+y are not. All these six arguments are separated using commas (,).
Whatever we write inside
" " or
' ' gets printed as it is and is not evaluated. So,
'is' got printed as it is. But
x+y are not written inside
' ' or
" ", as they are variables and the compiler will print their values.
That's why the value of
x (i.e., 10) got printed. Similarly, the value of
y (i.e., 5) got printed and at last, the value of
x+y got calculated (i.e., 15) and printed.
Now, let's get this more clear.
x = 10 print(x) print('x')
As you can see,
'x' printed x and
x printed 10. This means that
'x' is a simple alphabetical x and
x (without quotes) is a variable with a value of 10.
x = "Hello World" print(x) print('x')
x is a variable which stores a value of "Hello World", so
print(x) printed Hello World. Whereas,
'x' is simple x and thus,
print('x') printed x.
By practice, you will learn these things more effectively. So, make sure to solve questions from the Practice Section.
Let's have some fun
Try the following code.
print("a" + "b")
Let’s have a look at another example.
The message to be printed is equivalent to "Fun"+"Fun"+"Fun".
This is the Pythonic way to get your things done. These types of features of Python can be of good use for you in the future.
Let's Do Some Error Analysis
Do you know what a harsh reality is? The more you write code the more errors you get.
Do you know what makes a programmer a ‘great programmer’? There are many things but one of the most critical ones is the ability to hunt down bugs/errors.
So, let’s start with the first step of analyzing an error.
What if we type this :
x = 10 prin(x)
We have learned about
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'prin' is not defined
Let’s analyze this error and extract some information which is useful for us to fix this.
This is telling us that there is an error in line 1.
NameError: name 'prin' is not defined → 'prin' is nothing for our program because it is not defined anywhere in our program. That's why it is giving us an error.
NameError is a type of error. Python has few pre-defined error types. You can also make your own. We will look at it in a later chapter.
Go back to your code and then to that line (
prin(x)) and fix it.
In this chapter, we mainly learned about printing something on the screen. We also printed values of variables. So, let's move to the next chapter and learn what more we can do in Python.
It is necessary to solve questions and practice. As you progress in this course, it will be very difficult to understand concepts if you are not practicing. So, it is highly recommended that you go to the practice section regularly.