You are on page 1of 10

Python does not use a Compiler...it uses an Interpreter.

After you have downloaded Python onto your


computer (how to is at the end of this book) write your programs in Notepad and save them with the .py
extension, as in: ProgramOne.py Or write and save them first, then download Python, who cares? At any
rate, Python is the language that Yahoo, Youtube and Google use and it can be used to dastardly hack
into Facebook accounts so you know it is not going away any time soon. It was named after the idiotic
British movie "Monty Python and the Holy Grail" and if you never saw the movie here are some quotes
(which Pythoners are encouraged to include in their code):
"That is some killer rabbit!" "You will bring me some shrubbery!" "Holding thus the holy hand grenade
you pulleth out the pin and count to three." "What is the airspeed velocity of an unladen swallow?" And
they bang cocoanuts. The movie ends abruptly, has no end credits and people remain in the dark for a
good ten minutes before they realize what has happened. Somebody who had seen the movie before
stood up, called us all idiots and walked out.
The uniqueness of Python is that the {} symbol seen in related languages like C++ has been instead been
replaced by indented paragraphs.
These are characters: xa@_!?#*
This is a string: "xa@_!?#*"
Or the string, instead of double quotes could have single quotes surrounding the characters. Whichever
you use, keep them in pairs. It is better to use double quotes because these 3 single quotes right here
would confuse the Interpreter: ('Nobody touches Pete's piece') In this case, if you were bent on using
single quotes, to make the Interpreter happy you could write: ('Nobody touches Pete\'s piece'). See the
backwards slash? The backwards slash will cause the Interpreter to only see two single quotes. It is also
used as \n to skip a line when printing. And as \t for a tab jump.
This is a variable: variable=input('Enter a number here')
This is a single line comment the Interpreter won't see:
#A single line comment the Interpreter won't see because the computer is stupid.
Here is a multiple line comment the Interpreter won't see:
" " " A multiple line comment the Interpreter won't see
Because I am brilliant and the computer is stupid, aren't you, computer.
And I am sexy, also.
"""
[So, let us do now our first program in Python.
The input code will be just two lines:]

#This is a command to print Hello World


print ("Hello World")

[The ouput will be:]

Hello World

***********************
Next on our learning agenda is how to combine strings.
[Input]

Print ("I" + " love" + " you")

[Output]
I love you

************************

[Input]

first= "I"
second= "love"
third= "Monty Python"
sentence= first + ' ' + second + ' ' + third + '. '
print (sentence)

[Output]

I love Monty Python.

**************************
I order to combine both a string and a number together, you first have to turn the number into a string.

[Input]

version = 3
print ('I love Monty' + str(version) + '. ')

[Output]

I love Monty3.

***************************
There are times you will have to act interactively with the computer,
entering information when the computer asks you for it, like in polls. Tell it you are going to vote
Democrat. (No, not really :)

[Input]

person = input('Enter your name: ')


print('Hello', person)

[Output]

Enter your name: [you type in Fred, hit return]

Hello Fred

*****************************
[Input]

person = input('Enter your name: ')


print('Hello, ' + person)
age = input('Please enter your age: ')
print('Hi again, ' + person + ' ! I see you are' , age, 'years old. ')

[Output]

Enter your name: [you type in Fred]


Hello, Fred
Please enter your age: [you type in 33]
Hi again, Fred! I see you are 33 years old.

*******************************
Now is a good time to talk about Operators. They are:

== equal to
> greater than
>= equal or greater
< less than
<= equal or less than
!= not equal
<> another way of saying not equal

********************************
Let us also bring in arithmetic operators at this time. The order of math is from left to right. With the
exception of exponents which go right to left. Here they are, in order of how they are seen by the
computer, first performed at the top:
( ) brackets
** exponents
* multiplication
/ division
% modulus [It returns the remainder]
// floor [It removes the remainder]
+ addition
- subtraction
**********************************
[Input]

print (' - ' * 10)

[Output]

----------

***********************************
[Input]

weirdness = 'weird' * 3
print (weirdness)

[Output]

weirdweirdweird

*************************************
Example of how Modulus % works

[Input]

17%5

[Output]

**************************************
Example of Floor //

[Input]

17//5

[Output]

****************************************
Functions such as Length, Uppercase, Range, Count, Content check, Sorting, Max:

The Max Function

[Input]

max(3, 17, 5, 0)

[Output]

17

*****************************************

Content Check
a = [1,2,3,4,'a','b','c']
return 'a' in a

[or you might use]

if 'c' in 'Python':
print 'YES'
else:
print 'NO'

******************************************
Count Function

[Input]

sentence = 'Mary had a little lamb'


sentence.count('a')

[Output]
4
*******************************************
Sorting Alphabetically Function

[Input]
mylist=['charlie' , 'able' , 'baker']
mylist.sort( )
mylist

[Output]

['able' , 'baker' , 'charlie']

*******************************************
Sorting Numerically

[Input]

mylist = [1, 3, 2, 4, 5]
mynewlist= sorted (mylist)
mynewlist

[Output]

[1, 2, 3, 4, 5]
********************************************

Uppercase Function. There is also a lowercase function available.

[Input]
fruit = 'apple'
print (fruit.upper( ))

[Output]

APPLE

********************************************

Range Function

[Input]

for i in range(5):
print(i)

[output]

0
1
2
3
4

Here I might point out that when Python counts a list, the first item in the list is given the numerical
value zero. And pay attention to how print(i), above, had to be indented, about four spaces in. You will
see indents like this in all loops.
**********************************************

Length Function

[Input]

fruit = 'apple'
print (len(fruit) )

[Output]

5
[apple has 5 characters]
**********************************************
Writing to a file. Maybe there is a file you want to write stuff to.

[Input]

with open ('myfile.txt' , 'w') as myfile:

myfile.write ("we attack at dawn!")

***********************************************
Maybe there is a a file you just want to read.

[Input]

with open ('myfile.txt' , 'r' ) as myfile:

data= myfile.read ( )

[editor's note: data is a variable]

***********************************************
Tuple Assignment. A Tuple is a fixed list.

[Input]

Fred = ('Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' , 'Sunday')

Fred = (mon, tue, wed, thurs, fri, sat, sun)

print (mon)
print (fri)

[Output]

Monday
Friday

**************************************************
The difference between While and If Else loops is
that While asks questions many times over ...
If Else just asks a question once.

Ps: do not capitalize while and if else


and watch the indentations in each loop.
**************************************************
While Loops
***************************************************
[input ]

n = 100
s=0
while s<n:
s=s+1
print (s, "bottles of beer")

[output ]

1 bottles of beer...
.............................
100 bottles of beer

***************************************************

[Input]

i=1
s = 18
while (s > 17) and (s < 21):

print ( "you need a license")


s=s+1

[ Output]
you need a license
you need a license
you need a license

***************************************************
If Else Statements

[Input]

n = 21
if (n < 18) or (n >65):
print ("no license needed")
else:
print ("license needed")
[Output]
license needed

****************************************************

[Input]

mark = 78

if mark >= 90:

print ("A")
else:
if mark >= 80:

print ("B")
else:
if mark >= 70:

print ("C")
else:
if mark >= 60:

print ("D")
else:

print ("F")

[output]
C

*****************************************************
Here is the previous If Else program in Elif format:

[Input]
mark = 78

if mark >= 90:


print ("A")

elif mark >= 80:


print ("B")

elif mark >= 70:


print ("C")
elif mark >= 60:
print ("D")

else:
print ("F")

[output]
C

******************************************************
How to download the Interpreter:

For PC's go to
https://www. python.org/downloads/

for macs go to
https://www. python.org/downloads/mac-osx

When the download completes, double click on the .exe file to install it.

I went to the apple store and downloaded the app for my ipad: pythoni 3.3
Works great.

PS: All the code here was run on pythoni 3.3 to make sure they are accurate.

******************************************************
Other crazy sixties comedies that you might watch and base a whole
flippin code on:
The Fearless Vampire Killers (In Romania, Jewish vampires not held off by crosses)
Casino Royale (everybody is James Bond, even the dog)
What did you do in the War, daddy? (When people get drunk in Italy, it's make love not war)
What's New, Pussycat? (20 crazy people run wild across France...they can take it)
Those Magnificent Men in Their Flying Machines (Wright Brothers, move over)
The Great Race (American Victorian Feminist races with Dudley Do-right and Doc Doom)
Barbarella (In the year 5,000 A.D. Jane Fonda takes a space walk in the buff.)
It's a Mad Mad World (Bonzo Californians race to Santa Rosita wrecking everything and the police are
told not to interfere)

You might also like