Some Very Basic Python

Hello readers,

Rather than trying to wow you with some mega complicated code snippets; which you might not understand, and I probably couldn’t really explain, I figured maybe on occasion I’d throw out some very basic programming to encourage some interest.

Python is a scripting language, and with a simple interpreter it can do some really useful stuff.

To grab an interpreter and install python you should visit python.org or if you are on Linux you can check to see which version of python you have installed via command line by typing: python –version .

For the purpose of this tutorial I was using Geany on Linux Mint 18 and python 2.7 as well as Ninja IDE (after I found it and discovered that I liked it.)

First things first, remember to save whatever python code you start scripting with the .py extension. All languages have specific extensions that denote which type of document they are and python is no exception.

Some Basics

i = 1000
while i >=1:
print(i)
i = i – 1
print (“counting”)

The above script counts forward to 1000 and says counting between each number. In this case we are telling the compiler that i should equal 1000, and that while i is less than or equal to 1, it should count forward while printing the value as well as the word counting. The syntax here is simple enough, an equals sign means equals by itself, a > means less than, while a < means greater than. The semicolon declares the while statement but could also declare an if statement, or an else statement.

That all seems confusing but you’ll get a better understanding of it as we go.

def countdown():
i=5
while i > 0:
yield i
i -= 1

for i in countdown():
print(i)

In this case we are counting down instead of up. We’ve removed the word counting from our code and now it’s only counting down from 5. The yield command is important here as without it the countdown would continue forever. (Unless we stop it by pressing control c.)

Indentation is important but the editor will chide you into correcting it.

The python standard libraries are available online, by version. There is a drop down box on this page that displays all of the different versions.

If you found this article compelling enough go check out sololearn.com and before you know it you’ll have dozen or even hundreds of example scripts to tinker with. This was just after a few days of playing with what they teach.

Screenshot from 2016-09-06 19-37-56

 

I not only am not stopping anytime soon, I’m just getting started. Maybe you are too?

*Collected from around internet a neat code snippet (I didn’t write it but it worked in python 2.7)

Port Scanner

#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime

# Clear the screen
subprocess.call(‘clear’, shell=True)

# Ask for input
remoteServer    = raw_input(“Enter a remote host to scan: “)
remoteServerIP  = socket.gethostbyname(remoteServer)

# Print a nice banner with information on which host we are about to scan
print “-” * 60
print “Please wait, scanning remote host”, remoteServerIP
print “-” * 60

# Check what time the scan started
t1 = datetime.now()

# Using the range function to specify ports (here it will scans all ports between 1 and 1024)

# We also put in some error handling for catching errors

try:
for port in range(1,1025):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
print “Port {}:      Open”.format(port)
sock.close()

except KeyboardInterrupt:
print “You pressed Ctrl+C”
sys.exit()

except socket.gaierror:
print ‘Hostname could not be resolved. Exiting’
sys.exit()

except socket.error:
print “Couldn’t connect to server”
sys.exit()

# Checking the time again
t2 = datetime.now()

# Calculates the difference of time, to see how long it took to run the script
total =  t2 – t1

# Printing the information to screen
print ‘Scanning Completed in: ‘, total


 

Spread the love

113 thoughts on “Some Very Basic Python”

  1. Great post and right to the point. I am not sure if this is really the best place to ask but do you guys have any ideea where to hire some professional writers? Thanks :)

  2. Hiya, I am really glad I’ve found this information. Today bloggers publish just about gossips and net and this is really annoying. A good web site with exciting content, that’s what I need. Thank you for keeping this site, I’ll be visiting it. Do you do newsletters? Can not find it.

  3. I in addition to my pals ended up viewing the great things found on your web page and then unexpectedly developed an awful suspicion I never thanked the website owner for those strategies. All the young men were glad to study them and have now actually been having fun with them. We appreciate you actually being simply kind and for settling on variety of notable issues millions of individuals are really desperate to be aware of. My honest apologies for not expressing appreciation to you sooner.

  4. Fantastic beat ! I would like to apprentice while you amend your website, how could i subscribe for a blog website? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear concept

  5. As I web-site possessor I believe the content matter here is rattling excellent , appreciate it for your hard work. You should keep it up forever! Best of luck.

  6. Of course, what a fantastic blog and revealing posts, I definitely will bookmark your site.Best Regards!

  7. I happen to be writing to let you know what a brilliant experience our child found going through your web site. She came to find many things, which include how it is like to have a very effective helping nature to let other folks completely master specific complex subject matter. You actually surpassed visitors’ desires. I appreciate you for presenting these productive, healthy, explanatory and in addition cool tips on your topic to Lizeth.

  8. I was just seeking this info for a while. After 6 hours of continuous Googleing, at last I got it in your site. I wonder what’s the lack of Google strategy that don’t rank this kind of informative sites in top of the list. Generally the top web sites are full of garbage.

  9. There is perceptibly a bundle to identify about this. I consider you made certain nice points in features also.

  10. I simply could not go away your website prior to suggesting that I really loved the usual info a person supply in your visitors? Is going to be again steadily to check out new posts

  11. Undeniably believe that which you said. Your favorite justification appeared to be on the net the easiest thing to be aware of. I say to you, I definitely get irked while people consider worries that they plainly do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people could take a signal. Will likely be back to get more. Thanks

  12. you may have a fantastic blog here! would you prefer to make some invite posts on my blog?

    https://adrian55.joomla.com/2-uncategorised/3-points-on-ssc-cgl-exam

Leave a Reply

Your email address will not be published. Required fields are marked *