top of page

Our Recent Posts

Archive

Tags

No tags yet.

Python

What is python? Other than being a massive snake, it is a multifaceted scripting language that is powerful, versatile, and fairly easy to use. The syntax for the language is very straightforward and most of the Object Oriented features within can be compared to Java and other programming languages.

Python can be used on any computer within the command prompt. You can also go and look for a fancy IDE to use for more complex classes and programs you write. For the sake of explanation, we will just refer to the command prompt. Once you download Python from python.org (you can use v2. or v3.) simply go into your command prompt (or terminal on MAC in this case) and type: python. You should see syntax similar to this.

Austins-MacBook-Pro:~ leroyqb$ python

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

>>>

Once you see this, you will be good to begin scripting in python for as long as you like. Where all programmers like to start is printing the simple “Hello World!” string. To do this, all you need to type is

>>>print “Hello World!”

“Hello World!”

and then hit enter. Easy right? Well, I think the fascination for that ends just about as soon as it began. I want to touch on two other cool functions within Python and those are lists and dictionaries.

Lists are arrays of integers or strings that are stored in a specific order. It is very straight forward so I will just put some example syntax below. Some of the words used were .remove(), which removes a word from a list, .append(), which adds to the end of a list, and .pop() which removes the last word of a list.

>>> my_list = [“BOB” , “JIM” , “NANCY” , “TIM” , “JUNE”]

>>> my_list

[‘BOB’, ‘JIM’, ‘NANCY’, ‘TIM’, ‘JUNE’]

>>> my_list.remove(“JIM”)

>>> my_list

[‘BOB’, ‘NANCY’, ‘TIM’, ‘JUNE’]

>>> my_list.append(“JIM”)

>>> my_list

[‘BOB’, ‘NANCY’, ‘TIM’, ‘JUNE’, ‘JIM’]

>>> my_list.pop()

‘JIM’

>>> my_list

[‘BOB’, ‘NANCY’, ‘TIM’, ‘JUNE’]

>>>

Now lets look at dictionaries. Very similar to the list, but instead of just having a long array of data, each item in the array is considered a key value pair. This way, similar to an actual dictionary, you can search using a key word to get a piece of data linked to it. Lets look at an example below. I demonstrated how to add, remove and show the keys in a list.

>>> my_dict = {“SAM”:”Manager” , “JIM”:”Analyst” , “TIM”:”Accountant”}

>>> my_dict[‘JIM’]

‘Analyst’

>>> my_dict[‘ROB’] = “CEO”

>>> my_dict

{‘TIM’: ‘Accountant’, ‘ROB’: ‘CEO’, ‘JIM’: ‘Analyst’, ‘SAM’: ‘Manager’}

>>> del my_dict[‘JIM’]

>>> my_dict

{‘TIM’: ‘Accountant’, ‘ROB’: ‘CEO’, ‘SAM’: ‘Manager’}

>>> list (my_dict.keys())

[‘TIM’, ‘ROB’, ‘SAM’]

>>>

So that concludes the intro to python and the examples for today. To end all of your sessions in the terminal, simply type: exit(). All of these features and much more can be found on python.org under documentation.

Cheers!

bottom of page