1 / 15

Intro to Python

Intro to Python. IC322 Fall 2013. Starting Python. Command-line: C:Python32python.exe >>> GUI: ->All Programs->Python3.2->IDLE (Python GUI). Running Python. Interactive mode: >>>print (‘Hello’) Running python files: Start IDLE (It opens in Interactive Mode.)

Download Presentation

Intro to Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to Python IC322 Fall 2013

  2. Starting Python • Command-line: C:\Python32\python.exe >>> • GUI: ->All Programs->Python3.2->IDLE (Python GUI)

  3. Running Python • Interactive mode: >>>print (‘Hello’) • Running python files: • Start IDLE (It opens in Interactive Mode.) • File->New Window (Creates a new editor window.) • In the editor, type: print (‘Hello’) • File->Save As • Desktop/hello.py • Run->Run Module (or F5) to run.

  4. What is Python • Interpreted language • vs. compiled languages like C++ • Simple to use • Designed for programmer’s convenience, not the processor’s • Easy string and list manipulation * • Easy scoping rules • No memory management • Simplified variable typing • Extensible – you can create you own C++ libraries to import • Portable between Windows and Linux * >>> x = ‘this is a test’ >>> dir(x) >>> x.split()

  5. First program • Create the following program in your IDLE editor: print (‘Hello’) for i in range(10): print (i) • Press F5 to run

  6. Variables • Types include: • int • float • bool • string • objects • Type is implied, not specified • Types may be cast • Operations may include variables of different types: • float + int = float • float + string = <error> >>> x = 1 >>> type(x) >>> y = 1.1 >>> type(y) >>> z = ‘1’ >>> type(z) >>> x + y >>> type (x+y) >>> x + z >>> a = (int)(y) >>> type (a) >>> x + (int)(z)

  7. Whitespace matters • Leading whitespace in Python == {} in C++/Java • Run each of these programs and compare the output: • Program A print (‘Hello’) for i in range(10): print (i) print (i * 1000) • Program B print (‘Hello’) for i in range(10): print (i) print (i * 1000) • Now change the indentation randomly to see where it breaks

  8. Conditional Statements • Run the following: x = 1 if(x < 10): print (‘success’) else: print (‘failure’) • Try changing the second line to the following: if(x): if(x == 1): if(x != 2): if(x <= 1): if(not x==2): If(x == 1 or x == 2):

  9. Looping • Option 1: for i in range(10): print (i) • Option 2: x = 1 while(x < 10): print (x) x = x + 1 • Option 3: x = 1 while(1): print (x) x = x + 1 if (x>=10): break Removing which line(s) would cause an infinite loop? Remove it and press F5. Use Ctrl-C to stop the program.

  10. Functions • We can define our own functions: • Run the following: def countdown(x): for i in range (x, 0, -1): print (i) countdown(20) Type the following in the interpreter to learn about the range() arguments: >>> help(range) Note the complete lack of type-checking in the function definition. Try calling the countdown() function with a string or float value to see what happens.

  11. Importing libraries (modules) • We can import libraries that other people have written. • Libraries contain functions, classes, variables and libraries. • Start an interactive console and run the following: >>> import os >>> dir (os) >>> os.listdir (says listdir is a function) >>> os.listdir()(path is optional in Python 3) >>> help(os.listdir) >>> os.__file__ >>> os.__file__()(error) >>> type(os.__file__)

  12. Playing nicely with modules • Python does not have ‘private’ or ‘constant’ objects. • __private__  treat variables with __ as private • CONSTANT treate ALL_CAPS as constants • You can change anything in a module, but should not. • Start an interactive console and run the following: >>> dir() >>> dir (__builtins__) (what is in this module?) >>> __builtins__ = 1 >>> dir()(Why does this give you an error?) • Tip – do not set module values directly. Use module functions to set them.

  13. Two ways to import libraries • Run the following in a new python shell: >>> dir() >>> import socket >>> dir() >>> socket.gethostname() • Run the following in a new python shell: >>> dir() >>> from socket import * >>> dir() >>> gethostname() This method is preferred. Can you figure out two reasons why?

  14. Finding help on modules • List of all modules available for import: >>> help(‘modules’) • List of all functions/variables in a library: >>> import socket >>> dir(socket) • Description of entire library: >>> help(socket) • Description of a specific function or variable in library: >>> help(socket.gethostname) • Online reference for Python 3.2: http://docs.python.org/py3k/py-modindex.html

  15. Next Steps • The installed documentation has a good intro to python: •  ->All Programs->Python3.2->Python Manuals The Python Tutorial  An Informal Introduction • Here is the same information online: http://docs.python.org/py3k/tutorial/ Start in Chapter 3: An Informal Introduction to Python • You will need to be able to use the socket module for next week’s lab. Read about it here: http://docs.python.org/library/socket.html#module-socket

More Related