15
Lectures schedule 2 When ? Topic Lectu re October 26, 2014 Introduction to C Programming in Unix Environment - I 1 November 2, 2014 Introduction to C Programming in Unix Environment - II 2 November 9, 2014 Introduction to Assembly 3 November 16, 2014 Functions and System Calls (Assembly) 4 Midterm A (December 9, 2014 - 18:00) December 7, 2014 Unix Processes 5 December 14, 2014 Programs Execution 6 December 28, 2014 Introduction to script languages (Python) + ELF 7

Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

Embed Size (px)

Citation preview

Page 1: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

2

Lectures schedule

When ? Topic Lecture

October 26, 2014 Introduction to C Programming in Unix Environment - I

1

November 2, 2014 Introduction to C Programming in Unix Environment - II

2

November 9, 2014 Introduction to Assembly 3

November 16, 2014 Functions and System Calls (Assembly) 4

Midterm A (December 9, 2014 - 18:00)

December 7, 2014 Unix Processes 5

December 14, 2014 Programs Execution 6

December 28, 2014 Introduction to script languages (Python) + ELF

7

January 4, 2014 Web programming 8

Midterm B (January 19, 2015) OR (January 20, 2015)

Page 2: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

3

Revisit ELFNavigate through the Maze

Page 3: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

4

Current state

C and Assembly – Efficient code with access to OS services

Shell Scripts – connecting Unix utilities by I/O redirection and pipes

Page 4: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

5

Glue language

In practice, programs output needs pre-processing

Usually an interpreted scripting language

Useful for writing and maintaining: custom commands for a command shell wrapper programs for executables

C/Java VS. Python

Page 5: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

6

Exampleimport java.io.IOException;import java.io.FileReader;import java.io.BufferedReader;

class numlines {public static void main(String[] args) throws IOException {BufferedReader inp = new BufferedReader(new FileReader(args[0]));String line;for(int i=1;;++i) { line = inp.readLine(); if(line==null)

break; System.out.printf("%3d: %s\n", i, line);}inp.close();

}}-------------------------------------------------------------------------------------------import sys

i = 1for line in file(sys.argv[1]):

print "%3d: %s" % (i, line),i+= 1

Page 6: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

7

Python

An open source interpreted programming language

High-level language Object-Oriented Functional Programming

Suitable for scripting

Practical ( VS. Efficiency )

Dynamic language

Page 7: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

8

Variables

No need to declare

Not typed

greeting = "hello world"greeting = 12**2print greeting

Need to assign (initialize)▪ use of uninitialized variable raises exception

Page 8: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

9

Strings

"hello"+"world" "helloworld" # concatenation

"hello"*3 "hellohellohello" # repetition

"hello"[0] "h" # indexing

"hello"[-1] "o" # (from end)

"hello"[1:4] "ell" # slicing

len("hello") 5 # size

"hello" < "jello" 1 # comparison

"e" in "hello" 1 # search

Page 9: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

10

Tuples

key = (lastname, firstname)

point = (x, y, z) # parentheses optional

a,b,c = point # unpack

lastname = key[0]

sort of an immutable list key[1] = ‘Abed’ #TypeError: 'tuple' object does not support item

assignment

Page 10: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

11

Lists

[<elements>]

>>> my_list = ['Hello', 1, 2.2, 'world']>>> len(my_list)4>>> my_list[0]‘Hello’>>> my_list.append(‘espl’)>>> my_list >>> ['Hello', 1, 2.2, 'world‘,’espl’]>>> del my_list[0]>>> [1, 2.2, 'world‘,’espl’]>>> my_list.index(‘espl’)>>> 4>>> ‘espl141’ in my_list>>> False

Page 11: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

12

Dictonary

Hash tables, "associative arrays" d = {"duck": "eend", "water": “cold"}

Lookup: d["duck"] -> "eend" d["back"] # raises KeyError exception

Keys, values, items: d.keys() -> ["duck", “water"] d.values() -> [“eend", “cold"] d.items() -> [("duck",“eend"), (“water",“cold")]

Presence check: d.has_key("duck") -> 1; d.has_key("spam") -> 0

Page 12: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

13

Functions

def <function_name>(<arguments>): <body>

def factorial(n): fact = 1 for i in range(1, n):

fact = fact * i

return fact

Page 13: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

14

Classes

class student: def __init__(self, id):

self._id = id self._grades = {}

def update_grade(self, subject, grade): self._grades[subject] = grade

#Comment, Main:s = student('233445598')

s.update_grade('heshbon', 80) s.update_grade('handasa', 49)

Page 14: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

15

Modules

>>> from math import * >>> sin(30*3.14/180)

>>> from math import sin >>> sin(30*3.14/180)

>>> from math import sin as sinus >>> sinus(30*3.14/180)

Page 15: Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - // Extended

16

And More …

Control structures If statement While For ….