辅导案例-VSFX 160

  • May 15, 2020

Programming Introduction By Deborah R. Fowler VSFX 160 Programming Linux/Bash Houdini Programming != Math != Proceduralism Programming == Problem Solving • variables • truth statements • looping • functions • I/O • lists • classes/objects • OOP KEY CONCEPTS Python • interpreted • “less” syntax • all about format How will we use it? • IDLE on Windows • Linux • Tkinter/turtle • Many libraries Python Shell how do I get one? On Windows: Use IDLE (very basic interface or IDE – Integrated Developers Environment – Discuss) At Home: Type IDLE in the start menu At Monty: • double-click the idle.pyw in C:/Python27/Lib/idlelib • or right click any file with .py extension and select IDLE You can also create a simple python shell by clicking python.exe in C:/Python27 Python Shell How do I get one? Python Shell On Linux On Linux: Type python No official IDE is installed, you can use a text editor like gedit or geany At home you might want to try sublime To run a python program type python filename.py Introducing IDLE We have introduced two concepts: Variables and Operators Python x = 3 variables for storing information Operators Most languages have built-in mathematical operations + addition – subtraction * multiplication / division (be careful with integers) % modulus (remainder on integer division) variable literally means something that can change value stores data – all kinds – names, numeric values, memory addresses etc. in python they are not “typed” and do not need to be declared beforehand … variables What does that mean in python? You still must know what you are doing: x + y will not work if x is a string Operator awareness But 2 / 10 is .2 both integers we can fix it Modulus – remainder on integer division Relational Operators == equals != not equal < less than > greater than >= greater than or equal to <= less than or equal to Truth statements For selecting code if condition: do something if x == 3: y = 1 else: y = 2 truth statements – for selecting in Python if x == 3: y = 1 else: y = 2 x = 4 truth statements – add another line in else? Test in IDLE (don’t be fooled by the prompt when indenting) x = 4 if x == 3: y = 1 else: y = 2 NOTES to explain: • Formatting delineates blocks • python code is saved in a .py file • IDLE is an interactive shell • python is an interpreted language Python file ifexample.py Never be afraid/ashamed to look up syntax Your brain is needed for problem solving, not for memorizing rules As you use it daily, it will become familiar in-class exercise: Write a program that prints “Hello World” in a .py file in IDLE • variables • truth statements • looping • functions • I/O • lists • classes/objects • OOP KEY CONCEPTS homework: http://deborahrfowler.com/ pointers to information documentation/resources exercise instructions Turtle Graphics in Python By Deborah R. Fowler • variables • truth statements • looping • functions • I/O • lists • classes/objects • OOP KEY CONCEPTS Why I like Turtle Graphics: Programming with visual output Uses a Tkinter window (standard GUI – Graphical User Interface) http://www.deborahrfowler.com/PythonResources/PythonTurtle.html http://www.openbookproject.net/thinkcs/python/english2e/ or https://runestone.academy/runestone/books/published/thinkcspy/index.html https://docs.python.org/2.7/library/turtle.html Imagine a turtle with three attributes: location orientation a pen (color, width/up/down) in-class exercise: Draw one of your initials using the turtle library Looping – a way to repeat code In python for i in range(0,2): print “hello world” What would our square code look like? Other code What would happen if we left the last line off? Draw more than one square? We can nest loops in-class exercise: Given: circle(radius) – draws a circle of size radius fillcolor(colorname) – sets the color attribute for fill begin_fill() and end_fill() – similar to pu() and pd() Create a snowman – have fun, be creative Save your file and put it in the dropbox in a “Dailies” Folder in-class exercise: List is here: https://docs.python.org/2/library/turtle.html Functions A group of code statements Why? Allows us to organize and build modularly Allows easy repetition of code Turning our square into a function Adding parameters Calling with variable arguments • variables • truth statements • looping • functions • I/O • lists • classes/objects • OOP KEY CONCEPTS homework: Create a better snowman and read chapters 1-6 of the online resource: https://runestone.academy/runestone/books/published/th inkcspy/index.html You may start on the quilting exercise (E1) but it is not expected Instructions on the link on the course notes Programming Process by Deborah R. Fowler • variables • truth statements • looping • functions • I/O • lists • classes/objects • OOP REVIEW • Programming Process • Algorithms • Code Habits and details • Variables • Syntactic sugar Today Programming requires clear and careful creative thinking Programming Process Python algorithms Python In-class Exercises Concepts Organization Concepts learned are building blocks so ask questions early on! What is Programming? Telling the computer what to do • Problem solving • Algorithms - plan • Analysis – clear understanding of the problem • Design – identify key concepts involved in a solution • Program – express that solution in a language … how do you learn it? Studying good examples, practice, and experimentation As a programmer you are problem solving – defining the instructions (algorithm is the first step) In-class Exercise In-class Exercise (can be found in 18th century writings, although they use a wolf) Anaylsis – what can you leave behind? The dog does not eat cabbage Algorithm Start on side A Take the goat over to side B Return alone to side A Take the cabbage over to side B Return with goat to side A Take the dog to side B Return alone to side A Take the goat to side B Steps lead to completion Unambiguous Appropriate level of detail Well ordered instructions Covers all possible outcomes Algorithm is a general term for a clear concise finite set of instructions to solve a problem Vogel 1979 r = c * sqrt(n) theta = n * 137.508 • r is the radius from center • n is the number of the floret from center • theta is the angle Describes the head of a sunflower SCRATCH Here the pattern has different geometry to appear more like a sunflower Created in python Created in Maya/mel Houdini … with two expressions right in the interface … and more • Hscript expressions (houdini script) • Python expressions (as above but with python) • Python with HOM (Houdini Object Module) think PyMel • Vex (wrangle nodes/vex code) think rsl or C++ • L-systems (formal grammar) • And so on CODE HABITS Variable names • meaningful • add to the code readability • (self-documenting code) Not Good: hps, av, s Good: hitsPerSecond, average, score king_snake camelFace shorthand Syntactic sugar • * / % then • + - • Left to right in expression • Use () when in doubt! Order of Operators Precedence Build modularly! Test as you go! Summary Algorithm – a clear concise plan, not specific to a particular language syntax Habits • Human readable code – variables, comments • Modularity – build in chunks homework: You may start on the quilting exercise (E1) Functions by Deborah R. Fowler • Perform a task, then return control to your program (from where they were called) • Functions allow us to break up big programs into smaller pieces For example: def myFunction(): // things defined here exist only here (code block) // that is called encapsulation (it is a good thing) // statements to do something go here // called the body of the function return value // returns a value - optional The code is executed when the name is called someVariable = myFunction() myFunction(): // myFunction does something return value // return someVariable = myFunction() Some have a list here (parameters) where you send in information If it does, the call has a list (arguments) with information Called with num1 and num2 (5 and 7) Body of the function Two parameters a and b The number is 78125 So what does our mysteryFunction do? Unlike other programming languages there is no special significance to the word main, however it is a good habit as it helps us organize and remain consistent • A function is a reusable piece of code that is part of a larger program • Executed when it is called by another line of code How do functions help? • Modularity – repeating code effectively • Encapsulation – scoping/hiding – variables local - only exist within the block • Abstraction – don’t sweat the details Let’s look at our example again Let’s look at our example again The commented out lines all will produce an error This is because they are not accessible badidea is what is termed a global variable It is accessible everywhere Unless absolutely necessary global variables are not good coding style Homework: Continue working on your quilting exercise due Class 6 I/O by Deborah R. Fowler • variables • truth statements • looping • functions • I/O • lists • classes/objects • OOP KEY CONCEPTS • I/O • strings open close fileVar = open(filename,’r’) OR fileVar = open(filename,’w’) fileVar.close() RELATIVE VERSUS ABSOLUTE PATHS values[0] In Programming start counting at ZERO Strings print values[0] + values[1] would result in a string that was concatentated using float() to convert would result in 22.1 Positive from left Negative from right In-class Exercise Create a .txt file with a few lines of data Create a script to read the file and write out the second element of each line Summary of I/O (Input/Output) input open close strings are manipulatable Homework: Continue working on your quilting exercise due Class 6 – next class Lists by Deborah R. Fowler • variables • truth statements • looping • functions • I/O • lists • classes/objects • OOP KEY CONCEPTS Strings are lists print values[0] + values[1] would result in a string that was concatentated using float() to convert would result in 22.1 Positive from left Negative from right rstrip In-class Exercise Create a .txt file with a few lines of data, this time separate them with commas Create a script to read the file and write out the second element of each line Lists are defined by square brackets [ ] is an empty list ordered changeable Access an element or member of the list use an index (also called subscript) mylist[1] will give you the second item on the list remember to count from zero Why use them? Data structure that makes access easier, For example the “for” loop can be used to iterate through a list for item in mylist: print item append len Two other things python lists are capable of: • inserting into the list at a given position • differing types of items in a single list insert remove pop del Mixed types • variables • truth statements • looping • functions • I/O • lists • classes/objects • OOP KEY CONCEPTS Homework: Work on the Hurricane Exercise

LATEST POSTS
MOST POPULAR

ezAce多年来为广大留学生提供定制写作、留学文书定制、语法润色以及网课代修等服务,超过200位指导老师为您提供24小时不间断地服务。