When learning any programming language, you might start with printing ‘Hello, World!‘. This helps you understand the most basic structures of a language and also to check if everything’s working fine. So, why don’t you try this out?
If you didn’t install Python locally, try an online Python compiler, like this one.
Don’t forget to complete the assignment at the end!
The ‘Hello, World!’ program
Write down this line of code into your compiler and run it.
You could also do this:
And the compiler will give you this output:
2 things to notice here:
- The wrapping quotation marks will not get printed.
- print function ends with a newline character. That means, if you use 2 print functions, then the outputs will be displayed in 2 different lines, not in 1 line.
For this example, I’ll use my Linux terminal to execute the code. If you’re familiar with Terminal or Bash, open it and run ‘python3‘. Alternatively in Windows, open the command prompt (type CMD in the Windows search bar) and run ‘python‘.
To exit this python interface, run exit().
Understanding your first program
In your code, ‘print’ is a function. As you know, a function is something that performs some tasks. The print function prints something that is given to it. We use a function by using its name along with parentheses. That’s why you used print(). Inside the parentheses, you give it whatever you want it to display.
But why you used quotes?
We use either single(‘ ‘) or double(” “) quotes to wrap a text and tell Python that “it’s a text, just use it as it is, don’t try to find any meaning out of it”. In programming, we call text by String. More precisely, string is a data type, which represents a sequence of characters.
Should I use single quotes or double quotes
Whatever you like. Both have their benefits.
If the string includes a single quote to display, then wrap the entire string with double quotes. For example:
print("I'm a Python Programmer")
If you used single quotes to wrap it, the apostrophe of I’m would confuse Python about the ending quote. This will lead to an error. Try producing this error!
If the string includes a double quote to display, then wrap the entire string with single quotes. For example:
print('We are learning Python on "MLwiki" website')
I hope you understand now.
What if I need to display both single and double quotes?
In that case, you should tell Python to escape from considering a quote to be the ending of the string. How? Add an extra backslash(\) before the character you want Python to escape. Here’s how:
print('He said, "Can\'t join the party, I\'m correctly busy with a Python program".')
Here’s how it looks like:
He said, "Can't join the party, I'm correctly busy with a Python program".
It’s quite normal to make mistakes even in the simplest programs. Try producing them and understanding the error messages. Because you learn the most when you make mistakes.
- Case Sensitive
Python is case sensitive. So print and PRINT are 2 different names. Be careful!
It’s a NameError, meaning that Python can’t recognize this name. It’s also showing what the problem is, which is, ‘name PRINT is not defined’.
- Not closing the string
Whenever you start a string, Python looks for its ending. If not found, it raises error.
SyntaxError, means that your syntax is incorrect. Where’s the problem? ‘EOL while scanning string literal’. It means that when reading a string, Python couldn’t find the End Of Line (EOL).
- Not closing a function
print('Function not closing'
Remember we use parentheses when using functions? If you forget to use the closing parenthesis, then Python raises an error. You can’t produce this error in a command line or terminal, as the terminal will keep you asking for the end of the command. So I produced it in the online compiler suggested above.
Again, SyntaxError. What’s the problem? ‘unexpected EOF while parsing’. It means that Python reached the end of your code but could not found the End Of Function(EOF).
- Indentation Error
print('Unnecessary indentation was used here')
The structure of Python depends a lot on whitespace. Indentation in Python refers to the whitespaces (tab or 4 or 8 spaces) that are used at the beginning of a statement to give it a structure. You’ll learn about them in conditional statements or in loops etc. But, if you use unnecessary whitespaces before any statement, for example, the print() function, then Python will raise an IndentationError along with the statement where the problem occurred.
I hate this error, and you too will, very soon! 😆
Join the Facebook Group if You Haven’t Yet!
Use any text editor like IDLE(comes with Python), Nano, Vim, Sublime Text, Pycharm, or an online compiler, and try to solve these problems.
- Display 10 lines describing yourself. Hint:
print('line one') print('line two')
- Create a triangle with just print function. Output should look like this:
/\ / \ / \ / \ /________\
- Does this line make you happy?
If you’re new in programming world, specially in Python, try reading Python Programming – What and Why
You May Also Like: