Beginner introduction to Python – Part 1

In order to use the Django web framework I decided to review my knowledge of Python. It probably wasn’t required as Django seems user friendly enough but it couldn’t possibly hurt. In order to do this I used a Learn Python website. You can find it here. I reviewed courses on PluralSight as well. You can check out the course I used here. The learn Python website has really cool steps that give you a general view of the Python language. Again once you know how to program any other languages becomes a study in how and not what. So let’s get into it. Example files for this blog can be found here. You should really follow along with this one.

What you should know

Basically you should know how to program in at least one language. But Python is a pretty simple language so maybe you could learn on the fly. Point is I’m running on the assumption that we already understand programming.


You can download python from the following website

I downloaded PyCharm to create and run my python scripts. You can download the community version here.

Basically to install you can follow these instructions here. It’s literally the first article I saw. So if you find others that’s fine too.

Now you should be good to go. Lets get started.

To get started we created some variables. A string, int, Boolean, float.

stringCost = "20"  # string
intCost = 20  # int
isThisTrue = False  # boolean
price = 30.134 # float
bigPrice = float(50.00) # create a float
morePrices, lessPrices = 30, 50 # initialize multiple

Some things to note. The Boolean value had to be capital, it couldn’t or shouldn’t be false. We can also create a float using the float function. You can see how to initialize multiple variables from the last line.

We can add two strings together using the plus “+” sign.

spacedString = "I am adding" + "    " + "My own spaces"

Next we look at printing our variables to the console. To do this the print function was used. The print function can be used in many ways.

print("This is my first python script")

print("I am printing a string cost variable: " + stringCost)

print("I am priting a interger: " + str(intCost))

print("Show more and less prices " + str(morePrices + lessPrices))

print(morePrices, lessPrices)

Note that to print variables that aren’t strings we used the str function. This will convert them to string values. Also the last print statements shows another way the print function can be used. We can also concatenate values in the print function.

The second fun with py file deals with creating lists. Doing this is pretty straightforward.

myDogs = ["samuel", "matthew", "steve"]
myCats = []

Above we have a list with 3 items and myCats is an empty list.

We can add stuff to myCats using the append function.

myCars = []  # prints []

I also wanted to see how and empty list look like so I created myCars to print to the console.

When retrieving values from a list we can do the following

print(myDogs[2]) # prints steve
print(myCats[0]) # prints dory
print(myCats) # prints ['Dory', 'Lori']

Lists are zero index based of course. You can print the entire list if you wanted to as shown in the last line.

To get started we created our variables that we used in this example file.

car = 500
people = 100
peopleWhoHateDriving = 10
carNames = "Nissan"
myCarColors = ['red', 'blue', 'green']
yourCarColors = ['black', 'yellow', 'pink']

Next we experimented with some arithmetic operations. To multiply in python just use the “*” asterisk symbol.

peopleWithCars = car * people # prints 50000
squareThePeople = people ** 2 # prints 10000
cubeThePeople = people ** 3  # prints 1000000

Above we do two things we multiple car by people, we also square the amount of people. So we can use double asterisks to perform square operations. We can use the same style to do a cube operation.

The asterisks work differently on strings. If we wanted to create duplicates of a string we can use the multiplication operation. We can use the asterisks on list as well to get the same effect.

threeNissans = carNames * 3   # prints NissanNissanNissan
repeatMyColors = myCarColors * 3  # prints ['red', 'blue', 'green', 'red', 'blue', 'green', 'red', 'blue', 'green']

The minus sign is ” – “ and we can use it to perform a minus operation.

peopleNotDriving = peopleWithCars - peopleWhoHateDriving # prints 49990

The plus sign is ” + “ and works as expected. When used on lists you can concatenate a list.

allCarColors = myCarColors + yourCarColors # prints ['red', 'blue', 'green', 'black', 'yellow', 'pink']

The ” % ” sign performs a modulus operation.

remainderOfCarsFromThird = car % 3  # prints 2

The fourth fun with py file deals with string formatting. To get started we create the variables that we use in this file.

boy = "john"
girl = "elainea"
dog = "bobby"
age = 55
height = 25.33
dogTitle = "dogs name is %s" % dog
puppyNames = ["Barky", "Sparky", "Blacky"]

Once that’s done we can experiment with our strings.

print("boys name is %s" % boy)
print("girls name is %s" % girl)

We can use the %s to indicate we want to add a string in this location. Then next to the string with add another % sign and the variable to interpolate into the string. With the dogTitle variable you realize we did this while creating the variable.

We can have different formatting options

  • %s – Strings
  • %d – Decimals
  • %f – float
  • %.2f – formatted float – two decimal places

Some more examples of formatting are show below. We can add multiple values to format in string using brackets. The variables in brackets don’t need to be of the same type. In python language these variables in brackets are called tuples.

print("%s age is %d" %(boy, age))
print("%s height is %d" %(girl, height))
print("%s height is %f" %(boy, height))
print("%s height is %.2f" %(dog, height))
print("All the puppy names are %s" %puppyNames)

The fifth py deals with string functions and operations. You can view the initialization here. ( well the entire file )


We can use the len function to count the characters in a string.


We can use the index function on a string to find the first position of a given character in the string.

We can also splice strings base on positions.

print("My last Name " + myName[7:15])
print("My First Name " + myName[0:7])
print("Skip one letters: " + myProfession[0:18:2])
print("Skip two letters: " + myProfession[0:18:3])

The function – I guess – stringVariable[start:stop] starts at the start position and stops at the stop position. In between that you get your results. You can add an extra parameter like stringVariable[ start: stop: step ]. Step tells the function how to iterate over the string. In the last two print statements using 2, skips on letter and using 3, skips two letters.

Other functions on the string allow you to make the string upper case and lower case.


You can check to see if the string starts with or ends with a certain text input. These functions returns boolean values.


You can split a string by a delimiter which returns a list of the split string.

print(myName.split(" "))

The join function does something interesting when applied to the profession variable on the add sign it adds a plus sign between every character of myProfession. It does the same to lists. Every element of the list and plus sign will be added to.

print(addSign.join(myProfession)) # prints S+o+f+t+w+a+r+e+ +D+e+v+e+l+o+p+e+r
print(addSign.join([myName, myProfession]))

You can use the partition function to create a tuple with the elements on the left and right of the delimiter along with the delimiter.

print(ageString.partition(':'))  # prints ('Wynton Age ', ':', ' 53')

The format function allows you to add named variables to a string using a placeholder format {var}.

addMyAge = "Wynton is {age}"

You can add numbered placeholders like this

myPuppiesList = "I have three puppies {0}, {1}, {2}"
print(myPuppiesList.format("Bobby", "Hobby", "Tubby"))

The sixth py file deals with conditional programming with python.

Python doesn’t seem to like brackets or semicolons so their conditional statements reflect such disdain.

isDogReal = True
isDogFake = True

if isDogReal:
    print("Yes dog is reald")

if isDogFake != False:
    print("Dog is fake")

So above you can see how if statements are created. An if else statement is show below.

carRunning = False

if carRunning:
    print("Car is running")
    print("Car not running")

An if else statement using the or format is show below:

carRunning = False
doorOpen = True

if carRunning or doorOpen:
    print("Don't go outside")
    print("You can leave the car")

We can use the condition in statement for lists

aWallet = ["dollors", "cents", "cards"]
walletTag = "cardss"

if walletTag in aWallet:
    print("You have " + walletTag)

We can compare strings using the double equal operators.

yourPassword = "stupidLength"

if yourPassword == "stupidLength":
    print("This password works")
    print("this password doesnt work")

We can do an if else statement using an else if condition.

counter = 10

if counter == 4:
    print("counter = 4", "- Is counter 4")
elif counter == 5:
    print("counter = 5", "- Is counter 5")
    print("Counter is whatever it is")

We can use the not statement to negate a Boolean value.

isDogFake = True

print(not isDogFake, "- is dog fake")

We can check if two objects are equal using the is operator.

print(numbersObj is numbersOtherObj, "-Is on numbers")

To be Continued…

Part two will cover more topics on Python. Part three will host some cool examples using the python language and imported modules. After that I should finally get back to web development using the Django framework.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s