Data Center Handbook All things automation

Introduction to Python

In 1999 Guido van Rossum submitted a funding proposal to DARPA entitled Computer Programming for Everybody in which he described Python has having a ‘elegant syntax that is easy on the eyes and easy to learn and use’.

This principal of elegant, easy to learn syntax is plainly identifiable when comparing the classic Hello, World! program with Python and some of the other popular languages around today.

Java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

C

#include<stdio.h>

main()
{
    printf("Hello, World!");

}

Python

print('Hello, World!')

Without question, the above Python code block is the easiest to understand and reproduce.

Getting Started

In my experience, learning and understanding the following basic principals will enable you to get started on created Python scripts on a regular basis:

  • Variables
  • Input
  • List
  • Dictionary
  • For Loop
  • Conditional Statements
  • Functions

Variables

NAME = "Guido van Rossum"
LANGUAGE = "Python"

print('{} was created by {}.'.format(LANGUAGE, NAME))
# Results
Python was created by Guido van Rossum.

Input

NAME = input('Name: ')
LANGUAGE = input('Language: ')

print('{} was created by {}.'.format(LANGUAGE, NAME))
# Results
Name: Guido van Rossum  # 'Guido van Rossum' is supplied by the User
Language: Python        # 'Python' is supplied by the User

Python was created by Guido van Rossum.

Lists

LANGUAGES = ['Python', 'Java', 'Swift']

print(LANGUAGES)
# Results
['Python', 'Java', 'Swift']

Dictionary

LANGUAGES_CREATOR = {'Python':'Guido van Rossum',
                    'Java':'James Gosling',
                    'Swift':'Chris Lattner'}

print(LANGUAGES_CREATOR)
# Results
{'Python': 'Guido van Rossum', 'Swift': 'Chris Lattner', 'Java': 'James Gosling'}

For Loop

LANGUAGES_CREATOR = {'Python':'Guido van Rossum',
                    'Java':'James Gosling',
                    'Swift':'Chris Lattner'}


# key and value are arbitrary variable names
for key, value in LANGUAGES_CREATOR.items():
      print('{} was created by {}.'.format(key, value))
# Results
Python was created by Guido van Rossum.
Swift was created by Chris Lattner.
Java was created by James Gosling.

Conditional Statements

LANGUAGES_CREATOR = {'Python':'Guido van Rossum',
                    'Java':'James Gosling',
                    'Swift':'Chris Lattner'}

LANGUAGE = input('Language: ')

# key and value are arbitrary variable name
for key, value in LANGUAGES_CREATOR.items():
      if key == LANGUAGE:
          print('{} was created by {}.'.format(key, value))
# Results
Language: Python        # 'Python' is supplied by the User
Python was created by Guido van Rossum.

Where to go from here?

With these basic building blocks you will be able to start creating Python scripts for your day to day use. My recommendation is to find a task that you routinely have to do and then figure out a way to automate that process with Python. You won’t find a better tutor than trial and error. During this learning process your best friend is going to be Google.

If you’re looking for more formal training there a plethora of both free and paid options out there for you. If I were to do again I would start with the Learn Python Track from Treehouse.