70 likes | 89 Views
This introduction to Python covers installation, commands, functions, strings, lists, loops, and statements in an easy-to-understand manner. Learn essential Python syntax and start coding with confidence!
E N D
An Introduction to Python By Michael McCarthy
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
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) Getting Started
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 5 + 2J so (5 + 2j) * 10 = 50 + 20j) • 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) Uses of Symbols
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 Strings and Lists
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] Loops and Statements
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 Defining Functions