Python is known for its simplicity. So, let's tell the computer to say hello to us. ( Assuming that you have gone through the introduction section first and know how to write and compile python codes. If not, then go through introduction the section first. )
print "Hello World!"
And that's it!
To print any message on screen, just type print and that message in (' ') or (" "). Isn't it simple?
'print' is a function which prints something on the screen. (You will learn about functions in detail in later sections)
Let's see some more examples:-
For Python 3:
print ("Hello World!")
print "Hey! I am an upcoming coder" print "I am going to step into world of programming"
I am going to step into world of programming
print "I am loving this. Print, print and print!"
Let's do something more advanced.
x = 10 y = 5 print "sum of",x,"and",y,"is",x+y
For Python 3:
x = 10 y = 5 print ("sum of",x,"and",y,"is",x+y)
Here, x = 10
It means that we are taking a variable x and giving it a value of 10.
y = 5
Similar to x = 10, y is assigned a value of 5.
print "sum of",x,"and",y,"is",x+y
Let's learn about this line of code.
Whatever we write in (" ") or (' ') gets printed as it is and is not evaluated. So, 'Sum of' gets printed and then the value of 'x' gets printed (i.e. 10), then 'and' is printed as it is. Then value of y (i.e. 5) and at last x+y is calculated and printed. (i.e. 15)
Now, let's get this more clear.
x = 10 print x print 'x'
As you have seen, 'x' is a character and x is a variable. This means that 'x' is simple (x) and x (without quotes) is 10.
x = "Hello World" print x print 'x'
By practice, you will learn these things more effectively.
Let's do some error analysis
This part is only to teach you how to know the error if you are getting one.
What if we type this :
x = 10 prin x
Here, we are trying to teach you, how to fix an error, if you got one.
File "<stdin>", line 1 prin x ^ SyntaxError: invalid syntax
This is telling us that there is an error is in line 1 and '^' is pointing where it is.
Syntax error : invalid syntax
prin is nothing for our program. That's why it is giving us syntax error (as we know correct syntax would be print).
Go back to your code and then to that line (prin x) and fix it.
In future, when writing long codes, make sure that you run your code from time to time while writing instead of completing your whole code and running at last. This will make debugging your code easier if you have made some errors.
It is very 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.
Without practice, your knowledge is poison.