Download presentation
Presentation is loading. Please wait.
Published byEllen Powers Modified over 9 years ago
1
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1
2
Scripting Languages Useful for –system administration –Web programming –text processing, etc. 2
3
Scripting Languages Casual with regard to the typing of variables –Variables can be different types depending on the different information stored in them. –Different types were especially important when memory constraints were a bigger issue than they are today. –Integers take less memory to store than real numbers which take less memory than arrays. 3
4
Scripting Languages Arrays (which we haven’t defined yet) can use elements of different types - unheard of in most procedural languages –E.g.: Hello 227.8876 68 widgets $333.99 4
5
Scripting Languages Lots of built in high-level operations –String concatenation (making one large string by "fusing" two smaller strings) –Pushing an item onto a stack –Popping an item off of a stack 5
6
Scripting Languages Interpreted, rather than compiled –An interpreted language is executed "on the fly", one statement at a time Can be faster Often uses less memory Easier for using on machines with different operating systems 6
7
Scripting Languages Interpreted, rather than compiled –A compiled language is first translated into the machine language of the host machine and then executed. Limited by the host machine in use Uses more memory since it has to hold the entire program before it is executed 7
8
Why Python? Python & the Python-like Ruby have momentum in the scripting world. Cleaner More elegant Popular with developers – (Google uses Python) Easier to learn 8
9
Why Python? Pythonista - a Python aficionado Pythonistas advocate the use of Python for all programming, not just scripting 9
10
Interactive Mode Interactive mode - executing Python statements immediately Useful for quick checks of commands % python >>> prompt for a command … prompt when in a block … to finish 10
11
Case sensitive! Python is case sensitive –Upper and lower case letters are perceived as different things –Be careful of variables - especially if you use a text processing program that automatically capitalizes sentences –Ex: num & Num –Ex: print & Print 11
12
Formatting Notes Python is picky about some characters –Indenting must be with spaces, not tabs –Quotes must be non-smart double quotes (even though some versions say single or double quotes that match) –Ex: "Hello" or ‘Hello‘ –Ex: “Hello” or ‘Hello’ will each generate a syntax error –Ex: "Hello‘ - Error is hard to spot! 12
13
Definitions Block –Statements grouped together for a reason –indicated by a : at the end of the line. –Everything after MUST be indented the same amount. Be consistent. –Sub-blocks (blocks within blocks) are possible very useful. 13
14
Definitions List –A sequence of objects (can be different things) separated by commas and enclosed in square brackets. –Ex: [1, 2, 4, num] –Ex: ["hello", "goodbye"] –Ex: [23.5, $16.42, [0, -3], "string"] DO: Example 0 14
15
Definitions Variable –Letter or series of letters used to hold a value –Use descriptive variable names whenever possible Makes debugging SO much easier! –Use single letter variables for loop control only! (my rule – not a Python rule) 15
16
Definitions Functions –Call to a pre-defined mini program –Usually returns a value –Call with Funcname(args) –Ex: range(2) returns the list [0,1] –Ex: type(3) returns 16
17
Definitions Counting –In Python, counting starts at 0 –Becomes important for some functions (like range) DO: Example 0.1 17
18
Definitions Long lines –Continue on next line with a back slash –Ex: print "This is a very very very very very long"\ "line of text continued on the next line" 18
19
Order of Operations Mathematical calculations are executed in the following order: Expressions within ( ) Function calls Exponentiation - raise to a power - ** Multiply & divide * & / Add & subtract + & - 19
20
Order of Operations Ex: x= 2 print x**3 + 5 - 6.3*2 print 12 - (max(x, 6) /3) DO: FIRST, GUESS the Answer. Then, type in these statements. 20
21
Spacing Python is “very” forgiving of spaces! –Spacing around operators is optional –You can add spaces, or not, between the name of a function and the parenteses 21
22
Comments To put comments in a Python program just put # as the first char –Rest of the line is ignored –Ex: # ignore this line of code # print the results 10 times Comments will be necessary in ALL programs! (my rule) 22
23
For loop preview For loop - loop will execute a definite amount of times Ex: for i in [1, 2, 3, 4]: print "i=", i Ex: for x in range(9): print "loop” DO: Example 1A & 1B 23
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.