Introduction: Python for the Not-so Beginner Beginners

Hi,

last time, if you were paying attention, we touched on the basics of python - print, while and for loops, input & output, if, and a starter on easygui. also a free distribution of easygui and pycal-my own module.

this tutorial will cover:

more on easygui

decisions

functions

objects

comments

and modules

-quite a lot!

if you want to access my last instructable on the basics of python, please click here.

btw, the tabbing's gone wrong in the publishing, so click the image in each section to see how the program should look.

Step 1: More on Easygui!

recap:

the way to display a message box in easygui is:

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

import easygui

easygui.msgbox("hello world")

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

using buttonbox

here is a program that asks for your favorite color.

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

import easygui

color=easygui.buttonbox("choose your favorite color",

choices=['blue', 'red', 'green'])

easygui.msgbox("you picked" + color)

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

using choicebox

simple - just change the .buttonbox to .choicebox.

using enterbox

delete lines 2-3 on your buttonbox program and change it to:

color=easygui.enterbox("choose your favorite color")

setting a default

the way to set a default is to add a line below the enterbox program like this:

color=easygui.enterbox("choose your favorite color",

default='blue')

this way there will be a default word in the text box - you can delete it and write your own in, but you can just press enter if your favorite color is the default.

Step 2: Decisions

we've done something very similar to this - if statement. now we are going to make the computer decide.

type this program into a new code window dont type in the "<---"s and the text after it.

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

age=int(input("enter ur age: "))

year=int(input("enter ur grade: "))

if age>=12 and year>=7:<-------------------the and determines that only if both statements are true, it may continue.

print("you can play this game.")

else: <---------------------else makes sure that if the statements on the if arent applied, the code in else will proceed.

print("you cant play this game.")

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

the next function is elif. elif is short for else if. elif means that you can apply lots of steps to the program.

else

elif [if not true go to else]

elif [if not true go to the next one up]

elif [if not true, go to the next one up]

if [if not true go to elif]

Step 3: Functions

functions can save you from writing a line (sometimes lots of lines) over and over again each time you need to use it. to avoid this, you need the def() function. write this out in a code window:

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

def printmyadress():

print('Larry Zhang')

print('32 Holley Crescent')

print('Oxford, Oxfordshire')

print('OX3 8AW')

printmyadress()

printmyadress()

printmyadress()

printmyadress()

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

the four printmyadresses will print the lines in the "def printmyadress():" four times without you typing it all out four times.

Step 4: Objects

how would you describe an object? well that's exactly what we're going to do. we are going to type a program that describes a car, then, we are going to test it out with print(). for this, we need a new function - class. class is like a mega def, that includes lots of defs in it. type out the following lines into a new code window:

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

class car:

def drive(self):

if self.direction==("forward"):

self.direction=("front")

if self.fuel<=0:

print("no fuel left!")

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

now let's test the program, add the following lines onto the end:

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

c=car()
c.direction=("forward")

print ("my car is going "+c.direction)

c.drive()

print ("my car is facing "+c.direction)

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

the output should look like this:

==================================RESTART==================================

my car is going forward

my car is facing front

>>>

now we are going to set some default variables with __init__ .

add these lines before the def drive(self):

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

def __init__(self, direction, fuel):

self.fuel=(fuel)

self.direction=direction

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

now, let's see the full class with tests:

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

class car: <-----------lets python know that we are making a class
def __init__(self, direction, fuel): <----------------initializing the default variables

self.fuel=(fuel)

self.direction=(direction)

def drive(self):

self.fuel-=1 <-------------------take away one litre of fuel

if self.direction==("forward"):

self.direction=("front")

if self.fuel<=0:

print("no fuel left!")

c=car("forward", int(2)) <--------------sets the amount of fuel and the direction.

print ("my car is going "+c.direction)

c.drive()

print("my car is facing "+c.direction)

print("i have", c.fuel,"litres left.")

c.drive()

print("my car is facing "+c.direction)

print("i have", c.fuel,"litres left.")

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

phew! that was a lot to learn! don't worry if you don't get it the first time round - i didn't either! just keep on looking back!

Step 5: Comments

don't worry! i'm not giving you a report! comments are something in the program for programmers to see so they know what they're doing. they will not be run by the computer. type this line out:

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

#this is a comment

print("this is not a comment")

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

=============================RESTART=================================

this is not a comment

>>>

that should've been your output. you could put multiple lines of comments like this:

#**********************************

# this is how to write comments

# put a hash before each line

#**********************************

or if you want to write longer text and not to put a hash before each line, you could do it like this:

"""

blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah

"""

the triple-" method.

there is one problem with comments. let's say you work as a software engineer in a company with many people and you and the others write a program. then you put some comments in it. the next person comes along and sees the program and adds some more comments then the same thing happens again and again. the program will end up with so many comments that you can't find a single line of code. this proves very difficult in the debugging procedures. the way to solve this is that if you ever write a program with lots of people involved, don't write comments, but write a .txt document or a word document that only you can access.

Step 6: Modules

in this step, i will teach you how to make your own modules!!! yay!!! so... what is a module exactly? a module is a function (remember the def()?) that can be used in other programs. for example easygui is a module and the module i created, called PyCal is also a module. i am now going to teach you how to create something like pycal.

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

#this is the module my_module

def f_to_c(f):

return(5.0 / 9.0 * (F - 32))

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

save it as my_module.py.

now let's test it:

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

import my_module

my_module.f_to_c(1)

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

you should've got something like this:

=================================RESTART=============================

-17.22222222222222

>>>

or you can import it like this

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

from my_module import f_to_c
f_to_c(1)

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

this will import a specific function.

introducing random!!!

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

import random

random.randint(1, 10)

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

you should've got something like this:

=============================RESTART==================================

5

>>>

randint types a random number between an allocated number.

what time is it?

let's type in the next program:

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

import time

print("see you in 5 secs")

time.sleep(5)

print("hi again")

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

turtle

type in the following program and run:

from turtle import *

forward(100) <--------------the number is the distance.

right(90)<--------------the number is the angle

forward(100)

right(90)

forward(100)

right(90)

forward(100)

this should successfully draw a square.

Step 7: What Next?

that's it for now guys! that was a lot to learn especially the object section. i really hope i have made the text clear and, as always, leave comments for things i can add or to improve on. i have already started to think of ideas for "python for intermediate programmers" so you won't have to wait for long to learn more. if you still can't get easygui, i will give you the link here. go to step 6 and you will find a downloadable version of easygui and my own module, PyCal.

Bye For Now!