Quantcast
Channel: Infragistics Community
Viewing all articles
Browse latest Browse all 2374

Fundamentals of Python: Functions & Variables (Week 1)

$
0
0

Of course I can never take a break from packing my brain with more information, right?

Right.

In light of that revelation, I bring you my next Coursera foray into programming knowledge, Learn to Program: The Fundamentals, a class on the basics of Python.

Syllabus

This course provides an introduction to computer programming intended for people with no programming experience. It covers the basics of programming in Python including elementary data types (numeric types, strings, lists, dictionaries and files), control flow, functions, objects, methods, fields and mutability. Here is a tentative list of topics.

Week

Topics

1

Installing Python, IDLE, mathematical expressions, variables, assignment statement, calling and defining functions, syntax and semantic errors

2

Strings, input/output, function reuse, function design recipe, docstrings

3

Booleans, import, namespaces, if statements

4

for loops, fancy string manipulation

5

while loops, lists, mutability

6

for loops over indices, parallel lists and strings, files

7

tuples, dictionaries

 

Based on this syllabus, I’m hoping that what will happen in this course is similar to the last one – I’ll get a great refresher on some concepts that I’m already familiar with, learn the nuances of syntax for Python (which will be new to me!), and also, most importantly, add in new basic concepts to fill in remaining gaps in my current knowledge-base.

Onward and upward to Week 1!

As is pretty typical with any course, this week started out relatively slow, with some prep work to start. Let’s get right into it…

Lesson 1: Welcome to Learning to Program

This lesson was just a short intro to the class, basically covering the syllabus.

Lesson 2: Getting Started – Installing Python

Lesson 2 panned out pretty much as stated in the title. I had already installed Python/IDLE, so I skipped through most of this one.

Lesson 3: Python as a Calculator

Finally we’re getting into some actual methodology! This lesson couples some basic computational expressions with an introduction to working in the IDLE shell. Typical expressions such as -, +, * and / (as with most computer languages, / results in a float) work as expected. New notes are ** for exponentiation and // for integer division. Modulo (%), also works as expected, and operations are performed from the left to right of the equals sign, except in the case where order of operations takes precedence. As usual, ()s will override typical order of operations.

The lesson goes on to give a specific definition to syntax, which I thought was really interesting. The given definition here is that “Syntax is the rules that describe valid combinations of Python symbols,” and that a SyntaxError is a combination of symbols that can’t be evaluated by IDLE.

These SyntaxErrors are in contrast to SymanticErrors, which occur when the *meaning* of an expression is invalid, for example, when you attempt to divide by 0.

Lesson 4: Python and Computer Memory

(Note: It’s explicitly noted that this course will be operating on a “simplified” memory explanation.)

Memory is defined here as “a long list of storage locations, identified with a unique location which is known as a memory address. In the case of variable usage, a variable contains a named location in memory, such as x201, not an actual value. The value is stored in the memory address that the variable points to. Here’s the specifics that are given, which I think explain it a bit more clearly:

A Value HAS a memory addres.

A Variable CONTAINS a memory address.

A Variable REFERS to a value.

A Variable POINTS to a value.

Lesson 5: Variables

Variables are created using assignment statements, such as base = 20 or area = base * height. When a variable changes, the memory address it points to changes, NOT the value at the initial memory address. I think this distinction helps with the previous lesson’s specs.

Rules for Assignment Statements

  1. Evaluate the expression on the right of the = sign to produce a value. This value has a memory address.
  2. Store the memory address of the value in the variable to the left of the = sign.

Legal Variable Names in Python

-          Starts with a letter or _

-          Must contain only letters, digits, or _s

-          Case Sensitive!

Lessons 6: Built-In Functions

As with most languages, Python has a selection of functions that are already written and ready for the developer’s usage. What are they? Well… try this to find out!

dir(__builtins__) <- this="" calls="" dir="" which="" is="" a="" builtin="" as="" well="" produces="" the="" list="" of="" built="" in="" functions="" available="" python="" p="">

help (builtinnamehere) <- produces="" the="" details="" on="" builtin="" that="" you="" put="" within="" s="" p="">

Annnnddd you should be able to figure out everything you need to know about your builtin from there!

This lesson also reviews the rules for executing a function call, as described below:

Rules for Executing a Function Call

  1. Evaluate arguments from left to right, one at a time.
  2. Call the function, passing in argument values

Lesson 7: Defining Functions

I’m going to draw you some pretty pictures for this one. YAY!

First, an example with some color coded notes:

Next, just the color coded context example:

Also included in this lesson are…

Rules for Executing Return Statements

  1. Evaluate the expression, which produces a value
  2. Produce the value as a result of the function call

Nice and simple!

But wait… The function calls we went over in Lesson 6 are actually treated expressions!

What that means is that there are actually more advanced rules…

Advanced Rules for Executing a Function Call

  1. Evaluate arguments to produce a memory address.
  2. Store memory addresses in corresponding parameters.
  3. Execute the body of the function.

Annnnddd there you have it! Phew.

Week 1 Summary

This week went pretty much as expected, which is great because I’m always anxious when getting into a new topic. There’s been some great review of old topics, and even a few new bits in here for week one, like the ** for exponentiation and // for integer division. Overall, I’m definitely excited to see what this course brings and learn some new tricks!

Questions/Comments?

Feel free to comment here on my blog, or find me on Twitter @DokiDara.

By Dara Monasch

 


Viewing all articles
Browse latest Browse all 2374

Trending Articles