Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Python

Similar presentations


Presentation on theme: "An Introduction to Python"— Presentation transcript:

1 An Introduction to Python
By Michael McCarthy

2 Little Known Fact About Python / Attention Grabber
The language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles. Little Known Fact About Python / Attention Grabber

3 In order to use Python you must install the interpreter
One way of starting the interpreter is to use: python -c command [arg] This will execute the code within “command”. If you have spaces in your commands you should surround them with single quotes. Functions are used by the command function(object) (for example len(‘bar’) would return the length of the string bar which is 3) len(‘bar’) Getting Started

4 Uses of Symbols Comments are preceded by #
Mathematical operators work as normal (+ means add, - means subtract, etc) An equal sign (=) assigns a value to a variable (i.e., foo = 5 assigns the value 5 to variable foo) Unlike most programming languages that end either lines of code with ; Python’s lines of code are not terminated with symbols Imaginary numbers are followed by j or J (i.e., 5 + 2j or J so (5 + 2j) * 10 = j) When the interpreter is in interactive mode the answer from the last command is stored in the variable _ (if your last command was 5+2 then print _ would output 7) 5+2, 5*2 foo=5, bar=5, foo*bar _/1 5 +2j 2*_ Uses of Symbols

5 Strings are encapsulated in either single or double quotations
Strings are encapsulated in either single or double quotations. For strings spanning over multiple lines each line should be terminated by a \ character to indicate that there is a continuation. This is separate from using \n for a new line Putting r before stating a string converts the text to raw which will evaluate slashes and \n as text Putting u before a string indicates that a unicode string is being created Strings can be combined together with + and repeated with * Strings can be subscripted (slice in some languages) with []. Variable[0:2] would output from the beginning to the 2nd letter. Variable[1:] would output all but the first letter and Variable[:3] outputs the first 3 characters. Lists are similar to other languages – list = [1,2,3] or list = [‘a’,’b’,’c’] Len(list) outputs 3 For example: test = “Foo” test2 = “bar” test = test + test2 would output Foobar test * 3 would output FoobarFoobarFoobar test[1:3] test[:2] test[2:] List = [‘a’,’b’,’c’] Len(list) Strings and Lists

6 The first line of Loops and statements end in colons (:) and there are no brackets used to denote the contents of your command In Python, pressing enter after the colon brings the user prompt to a new line where you must indent to specify the contents of your loop. Each command should be on a separate line with each line indented. To end the writing of a loop or statement simply press enter/return without any input if q > 5: q = q – 1 print q elif q == 3: print ‘q is 3’ else: print ‘q is other’ For statements are different then normal. For statements iterate over the items of the given sequence in order applying that value to your for variable A traditional for loop would be more like this: for i in range(len(list)): print len[i] num = 0 While num < 12: print num num = num + 2 if q > 5: q = q – 1 print q elif q == 3: print ‘q is 3’ else: print ‘q is other’ Var = [‘monkey’,’donkey’,’blah’] For el in var: print el Len = [‘one’,’two’,’three’] for i in range(len(list)): print len[i] Loops and Statements

7 Defining Functions Functions are defined as follows:
def name(variable): print variable We can rename functions by assigning them to variables: blah = name blah(‘monkey’) outputs monkey Loops and statements can be put inside of functions as normal Def name(variable): print variable Blah = name Blah(‘monkey’) Defining Functions


Download ppt "An Introduction to Python"

Similar presentations


Ads by Google