Home
|
Table of Contents
|
Overview Map
Comp 170 H1 - Spring 2015 - Steil
dsteil@harding.edu
dsteil@harding.edu
Home
>
Exam 4
>
Week 10
Previous
Next
Week 10
Monday
More about Pointers
pointer - variables that hold address of other variables
declaring a pointer with *
de-referencing a pointer with *
find the address of a variable with &
Dynamic memory allocation with new, give it back with delete
Memory leak
Tuesday
Subtopic
Wednesday
Thursday
Friday
Python Introduction
Install from
https://www.python.org/downloads/
Idle - IDE that comes with python but something like notepadd++ or pyCharm is better
Python Shell - c:\python34\python.exe
Python Language
Interpreted language - Not compiled, slower
Dynamically type - no strict variable types, no need to declare variables just start using them
A white-space language - delaminated by spaces
Running from the command prompt
python programName.py
run the python program passing is the name of your .py file as the only command line argument
variables
no declaration
name must start with a letter
name may contain a letter, number, or underscore
5 general variable types in python
Numbers
Strings
Lists
Tuples
Dictionaries
Arithmetic operators
like c++ - +, -, * ,/ %
exponential - **
floor division //
order of operations is applied as expected
Simple examples
output -
print("Hello World")
output without new line -
print("Hello World", end = "")
input - myName = input("Prompt text?")
myName is a variable that holds some type of input
input number example
comment - line begins with #
No difference between single and double quoted strings. 'apple' is the same as "apple"
Conditional statements:
Everything inside the conditions should be tabbed over one tab space.
if elif else
Short circuit boolean expressions are applied
boolean operators "and" and "or"
Loops
Everything inside the loop should be tabbed over one tab space.
while
for
Functions
def functionName(parametesr):
Everything inside the function should be tabbed over one tab space.
"void " function example
function that returns a value
function parameter example
scope of variables made inside a function... it is only in the function
optional parameter
def fun(n = None):
test for none like this: if n is None:
import
mulit line string - ''' test that has multiple lines ''''
Just place three single quotes on each end of the text
lists
with indexes Can contain anything in one list
names = ['Emma','Eli','Eden']
there can be lists inside of lists (picture as boxes inside of boxes)
names.append ('Ezra') adds a new item to the end of the list
names.insert('Other',1)
names.remove('Other')
len(names) - finds the number of items in the list
min(names) - finds the number of items in the list
max(names) -
names.reverse()
names.sort()
del names[1]
for loop with list
tuples
useful when you have collections of things that are not likely to be changed
function parameter example
dictionaries -
example
random
import random
random.randrange(1,10)
random example
strings
substring first n characters - s[startIndex:length]
substring last n characters - s[-n:]
substring all but last n characters - s[:-n]
find index of string in string (case sensitive) - string.find("query")
s.isalpha() - is alpha
s.isalnum() - is all number
len(s) - length of the string
s.replace("text", "other text")
s.strip() - removes all white space
s.split(",") - makes a list of strings by parsing the original string where the commas where
File IO
file_handle = open("filename.txt","wb")
file_handle.write(bytes("Text to write to file", "UTF-8"))
file_handle.close()
file_handle = open("filename.txt", "r+")
text = file_handle.read()
os.remove("filename.txt") - deletes the file
OOP
class key word
__ - two underscores proceed private data member names
encapsulation
dog class
,
dog test
Resources
Python Beginner Tutorial 1 (For Absolute Beginners)
Python Beginner Tutorial 2 (For Absolute Beginners)
Python Beginner Tutorial 3 (For Absolute Beginners)
Python Beginner Tutorial 4 (For Absolute Beginners)
Python Programming (Derek Banas)
Top
Dana Steil PhD - Harding University
Last updated:
3/26/2015