Login Register




Tutorial Python for beginners | Printing stuff filter_list
Author
Message
Python for beginners | Printing stuff #1
Hello guys, I started a basic tutorial serious on Python . Hear I'm going to explain many theoretical and practical stuff in python programming. I'll explain basic things and also some deep stuff like object oriented programming, Ctypes, Network programming etc.

Printing is an absolutely basic part of a programming language. We learned how we can print a string with PHP and C. So in this tutorial we are going to talk about python printing. Hear I explain you both python 2 and 3. Let's see following program.

Code:
#!/bin/python
print ('welcome to HacksLand')
print ("this code's syntax is OK. But why?")
print ('guys, it/'s my first python code')
#this is the way we make a comment in python

"""
I am also a comment that wrap to many lines . But this commenting way is not official
"""

print ('I am ok in both python 2.x and 3.x')
print 'I will only run in python 2.x'

  First thing to understand is syntax of printing something. print ('string goes hear') We can use print() function to do this. You want to put the string as the argument for that function. (I'll explain more about functions in a later tutorial). What if you include a quot in your string? It will disturb Python's syntax.

Code:
Thilan@ubuntu:~/programming$ cat tmp1.py
print ('it's a python code')

Thilan@ubuntu:~/programming$ python tmp1.py
  File "tmp1.py", line 1
    print ('it's a python code')
              ^
SyntaxError: invalid syntax
Thilan@ubuntu:~/programming$ nano tmp1.py
Thilan@ubuntu:~/programming$ cat tmp1.py
print ('it\'s a python code')


Thilan@ubuntu:~/programming$ python tmp1.py
it's a python code
Thilan@ubuntu:~/programming$

In next line I opened a nano editor and corrected the error. After I ran it again and its working fine. So what the magic happened hear when we used a backward slash? Its a escape symbol. That mean when python interpreter see a backward slash it'll ignore next character. Actually this is not a ignoring. It think next character is a specific one and should not printed normally. Also there are some other escape characters . We use those sometimes in our programs. \n - This one is used to indicate a line break. If we use this between a string it will break and go to another line. \t - This acts as a tab space. \\ - We can print a backward slash with this. Why it happen?? Think.! Now you need to do experiments with those. Do it yourself. Next we have another problem. This string also uses a quote inside it. But it's not produce an error. print ("this code's syntax is OK. But why?") This is because hear we have used different quotation marks for indicate the string. If we want to make a comment we use '#' symbol. In a previous tutorial I have explained you why we use comments in our programs. Also we can use triple quotes for a multi line code. This is very helpful when we want to disable a code block for debugging purpose.

Code:
Thilan@ubuntu:~/programming$ cat tmp1.py
print 4 + 5 + 3
print 4 + 5 #+ 3
print ('HacksLand')#Ethical hacker's playground

Thilan@ubuntu:~/programming$ python tmp1.py
12
9
HacksLand
Finally I have listed another way to print a string. Hear we don't use parentheses. But this way is not correct in python 3.x version.

Code:
Thilan@ubuntu:~/programming$ python tmp1.py
I will only run in python 2.x
Thilan@ubuntu:~/programming$ python3 tmp1.py
  File "tmp1.py", line 1
    print 'I will only run in python 2.x'
                                        ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('I will only run in python 2.x')?

So it's a good programming practice to always use first method for python printing. Because it's important to learn both versions of python.
So guys that's all for now. see you in next post. thanks for reading.  Biggrin  Biggrin
(This post was last modified: 10-20-2019, 05:38 AM by thil4n. Edit Reason: my mistakes in grammar and spellings. )
B.Sc Engineering in computer science [Student]
Gov. university of Peradeniya.
Author at HacksLand.net Cool

[+] 1 user Likes thil4n's post
Reply

RE: Python for beginners | Printing stuff #2
man i would like to learn how to make checker in python with good explanation. if you know how to .

Reply

RE: Python for beginners | Printing stuff #3
(10-20-2019, 01:59 PM)fsociety Wrote: man i would like to learn how to make checker in python with good explanation. if you know how to .

What do you mean by a "checker" ?
B.Sc Engineering in computer science [Student]
Gov. university of Peradeniya.
Author at HacksLand.net Cool

Reply

RE: Python for beginners | Printing stuff #4
(10-20-2019, 05:19 PM)thil4n Wrote:
(10-20-2019, 01:59 PM)fsociety Wrote: man i would like to learn how to make checker in python with good explanation. if you know how to .

What do you mean by a "checker" ?

account checker i thought you know OK leave it.

Reply






Users browsing this thread: 1 Guest(s)