This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:bh011695:portfolio:pythontutorial [2012/02/23 20:27] – bh011695 | user:bh011695:portfolio:pythontutorial [2012/02/23 20:28] (current) – [Private Class Members] bh011695 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Python Programming Tutorial ====== | ||
+ | A few things to get started. First and foremost, Python syntax is very picky. Make sure all the indentation is correct and colons are in their places. Those two things, I would say make up for almost all of my syntax errors. Make sure to have # | ||
+ | < | ||
+ | This will give the user full read, write, and execute permissions but only execution for group and other. | ||
+ | ===== Print Statements ===== | ||
+ | |||
+ | Print statements are quite simple in python, almost too simple. One little command is all you need. | ||
+ | < | ||
+ | When using print statements in python, there is no need to enter a new line character to start a new line. New lines are entered automatically at the end of the print statement. If you don't want a new line when using multiple print statements, do something like the following: | ||
+ | <code python> | ||
+ | print " | ||
+ | This should give you the following output: | ||
+ | < | ||
+ | Hello, World! Goodbye, World!"</ | ||
+ | |||
+ | ===== Getting Input From The User ===== | ||
+ | To get some input from the keyboard, it's as easy as this. | ||
+ | <code python> | ||
+ | There is also the input() function. However, this seems to only work with numeric values. So, if you're looking to enter a string stick with raw_input(). Here's a simple example of getting input and displaying it to the screen. | ||
+ | <code python> | ||
+ | print " | ||
+ | </ | ||
+ | < | ||
+ | What is your name? John | ||
+ | Hello John | ||
+ | </ | ||
+ | |||
+ | ===== If Statements and Case Structures ===== | ||
+ | ==== Ifs ==== | ||
+ | |||
+ | If you've done any C programming, | ||
+ | <code python># | ||
+ | var1 = 1 | ||
+ | var2 = 3 | ||
+ | |||
+ | if (var1 < var2): | ||
+ | print var1, "<", | ||
+ | </ | ||
+ | |||
+ | Running the program should result in the following output: | ||
+ | < | ||
+ | 1 < 3 | ||
+ | </ | ||
+ | |||
+ | ==== Elses ==== | ||
+ | |||
+ | Now if you wanted to make this an if then else the code would look like this: | ||
+ | <code python># | ||
+ | var1 = 3 | ||
+ | var2 = 1 | ||
+ | |||
+ | if (var1 < var2): | ||
+ | print var1, "<", | ||
+ | else: | ||
+ | print var2, ">", | ||
+ | </ | ||
+ | |||
+ | Now the program' | ||
+ | < | ||
+ | 3 > 1 | ||
+ | </ | ||
+ | |||
+ | ==== Else ifs ==== | ||
+ | |||
+ | There are also the else ifs, which again, are not much different than doing them in C. | ||
+ | <code python> | ||
+ | print var1, "<", | ||
+ | elif (var1 > var2): | ||
+ | print var1, ">", | ||
+ | </ | ||
+ | |||
+ | As far as case structures are concerned, they don't exist as they do in C. In place of case structures, in python you would just use a bunch of if/elif statements. | ||
+ | <code python> | ||
+ | #do this | ||
+ | elif (var == 1): | ||
+ | #do this | ||
+ | elif (var == 2): | ||
+ | #do this | ||
+ | </ | ||
+ | |||
+ | ===== Loops ===== | ||
+ | ==== While Loops ==== | ||
+ | A simple while loop would look something like this: | ||
+ | <code python>x = 0 | ||
+ | X_MAX = 10 | ||
+ | |||
+ | while (x < X_MAX): | ||
+ | print x, | ||
+ | x = x + 1 | ||
+ | </ | ||
+ | |||
+ | <cli> | ||
+ | lab46:~$ | ||
+ | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | ||
+ | </ | ||
+ | |||
+ | If you want each number on a different line, remove the comma from the print statement. | ||
+ | |||
+ | ===== For Loops ===== | ||
+ | For loops, in python, come in various shapes and sizes depending on what you're doing. In most cases, this one will be fine for whatever you're doing. | ||
+ | <code python> | ||
+ | print x, | ||
+ | </ | ||
+ | This should give you the same output as the above while loop. | ||
+ | |||
+ | ====== Functions ====== | ||
+ | Functions in python consist of the function heading, the function body, and the return statement. Just like with variables, there' | ||
+ | <code python> def sum(a, b, c): | ||
+ | return a + b + c | ||
+ | </ | ||
+ | If you wanted to have a body to this function, it could also be done like this. | ||
+ | <code python> def sum(a, b, c): | ||
+ | d = a + b + c | ||
+ | return d | ||
+ | </ | ||
+ | |||
+ | ====== Classes and Objects ====== | ||
+ | If you've done problem solving or C++, classes in python may seem a bit odd. Unlike in C++, in Python there are no such things as private class members. Everything in a python class is a public member. Let's create a class called circle. | ||
+ | <code python># | ||
+ | |||
+ | class circle: | ||
+ | def __init__(self, | ||
+ | self.diameter = radius * 2 | ||
+ | self.circumference = self.diameter * 3.14 | ||
+ | self.area = 3.14 * radius * radius | ||
+ | | ||
+ | myCircle = circle(25.0) | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | </ | ||
+ | When you run this you should get the following output. | ||
+ | < | ||
+ | Diameter: 50.0 | ||
+ | Circumference: | ||
+ | Area: 1962.5</ | ||
+ | |||
+ | Think of the < | ||
+ | When you use the variable members make sure to use the following syntax: objectName.variableName or in the case of a class function objectName.classFunc(). | ||
+ | |||
+ | Another way to do this would be to not access your variables directly a la C++ and return values for diameter and circumference. | ||
+ | <code python> | ||
+ | # | ||
+ | |||
+ | class circle: | ||
+ | def __init__(self, | ||
+ | self.radius = radius | ||
+ | | ||
+ | def getRadius(self): | ||
+ | return self.radius | ||
+ | | ||
+ | def calcDiameter(self): | ||
+ | return self.radius * 2 | ||
+ | |||
+ | def calcCircumference(self): | ||
+ | return 2 * 3.14 * self.radius | ||
+ | | ||
+ | def calcArea(self): | ||
+ | return 3.14 * radius * radius | ||
+ | | ||
+ | myCircle = circle(25.0) | ||
+ | print myCircle.getRadius() | ||
+ | print myCircle.calcDiameter() | ||
+ | print myCircle.calcCircumference() | ||
+ | </ | ||
+ | |||
+ | When you run this you should get the following output. | ||
+ | < | ||
+ | Radius: 25.0 | ||
+ | Diameter: 50.0 | ||
+ | Circumference: | ||
+ | Area: 1962.5</ | ||
+ | |||
+ | A few things to remember when doing it this way. First, when you define your functions, all of them require the parameter " | ||
+ | |||
+ | ===== Inheritance ===== | ||
+ | Time for some inheritance. Inheritance can be fairly confusing in python. There' | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | |||
+ | class Person: | ||
+ | def __init__(self, | ||
+ | self.lastName = last | ||
+ | self.firstName = first | ||
+ | |||
+ | def printName(self): | ||
+ | print " | ||
+ | |||
+ | class Employee(Person): | ||
+ | def __init__(self, | ||
+ | Person.__init__(self, | ||
+ | self.ID = ID | ||
+ | self.hourlyRate = rate | ||
+ | self.hoursWorked = hours | ||
+ | self.grossPay = self.hourlyRate * self.hoursWorked | ||
+ | |||
+ | def printEmployeeInfo(self): | ||
+ | print " | ||
+ | Person.printName(self) | ||
+ | print " | ||
+ | print "Hours Worked:", | ||
+ | print "Gross Pay:", ' | ||
+ | |||
+ | brad = Employee(" | ||
+ | brad.printEmployeeInfo() | ||
+ | </ | ||
+ | |||
+ | ===== Composition ===== | ||
+ | Ahhh, composition, | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | |||
+ | class Name: | ||
+ | def __init__(self, | ||
+ | self.first = first | ||
+ | self.last = last | ||
+ | | ||
+ | def getFirst(self): | ||
+ | return self.first | ||
+ | | ||
+ | def getLast(self): | ||
+ | | ||
+ | |||
+ | class Person: | ||
+ | def __init__(self, | ||
+ | self.n = Name(first, last) | ||
+ | self.age = age | ||
+ | | ||
+ | def getFirstName(self): | ||
+ | return self.n.getFirst() | ||
+ | |||
+ | def getLastName(self): | ||
+ | return self.n.getLast() | ||
+ | | ||
+ | def getAge(self): | ||
+ | return self.age | ||
+ | |||
+ | brad = Person(" | ||
+ | print " | ||
+ | print " | ||
+ | </ | ||
+ | Most of this code should look fairly familiar. We create a class called Name that consists of a first name and a last name and methods to return the first name and last name. Immediately afterwards, we create a new class called Person which has an object created in its constructor called self.n to store a name. An object of type Person contains a first and last name and an age. The Person class contains methods to return the first name, last name, and the age. Getting the first name and last name involves calling the get methods in the Name class. When you run the code it should give you the following output. | ||
+ | < | ||
+ | Name: Brad Hammond | ||
+ | Age: 26 | ||
+ | </ | ||
+ | |||
+ | ===== Private Class Members ===== | ||
+ | It is also possible to create private data members within your classes using a certain naming convention. Normally a class member would look something like the following: | ||
+ | |||
+ | <code python> | ||
+ | self.name = arg1 | ||
+ | </ | ||
+ | |||
+ | To create a private member it would look like this: | ||
+ | |||
+ | <code python> | ||
+ | self.__name = arg1 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | <cli> | ||
+ | brad@dhcp-181:/ | ||
+ | Printing via function myClass.printStuff() Brad | ||
+ | None | ||
+ | Printing via direct access | ||
+ | Traceback (most recent call last): | ||
+ | File " | ||
+ | print " | ||
+ | NameError: name ' | ||
+ | </ | ||
+ | |||
+ | ====== Predefined Functions ====== | ||
+ | Python has a lot of really usefully built in functions used for various purposes. In most cases, they' | ||
+ | |||
+ | ===== Math Functions ===== | ||
+ | ==== abs() ==== | ||
+ | abs() returns the absolute value of its parameter. These can be integers, floating point values, or even complex numbers. If the parameter is a complex number the function returns the number' | ||
+ | <code python> | ||
+ | |||
+ | ==== complex() ==== | ||
+ | complex() converts its parameters to a complex number. Its parameters can be integers, floating point values, and the 2nd parameter may be imaginary. So it may take the following forms: | ||
+ | <code python> | ||
+ | complex(num1, | ||
+ | |||
+ | ==== pow() ==== | ||
+ | pow() comes in two forms. Form one is: | ||
+ | <code python> | ||
+ | Form two is: | ||
+ | <code python> | ||
+ | In form one, we simply raise x to the power y. The 2nd form is equivalent to (x ^ y) % z. | ||
+ | |||
+ | ==== round() ==== | ||
+ | round() is a bit screwy. It takes one or two arguments in the form of round(x, y). It rounds x to y decimal places. If y is not specified, it treats y as if you had entered a zero | ||
+ | <code python> | ||
+ | round(x)</ | ||
+ | |||
+ | ===== Data Type Conversion ===== | ||
+ | ==== bin() ==== | ||
+ | bin() takes an integer value and converts it into a binary number | ||
+ | <code python> | ||
+ | |||
+ | ==== bool() ==== | ||
+ | bool() converts a number into a bool value of true or false. A zero evaluates to false, anything other than zero evaluates to true. | ||
+ | <code python> | ||
+ | |||
+ | ==== float() ==== | ||
+ | float() converts a number into a floating point value. Entering 25 as a parameter will return 25.0. | ||
+ | <code python> | ||
+ | |||
+ | ==== hex() ==== | ||
+ | hex() converts an integer into a hexadecimal value. hex(632) returns 0x278 | ||
+ | <code python> | ||
+ | |||
+ | ==== long() ==== | ||
+ | long() converts an integer into a long integer value. | ||
+ | <code python> | ||
+ | |||
+ | ==== oct() ==== | ||
+ | oct() converts an integer number into an octal value. | ||
+ | <code python> | ||
+ | This returns 031. | ||
+ | |||
+ | |||
+ | Brad Hammond |