1 / 40

Learn IT 2014: Python

Learn IT 2014: Python. Basic programming. Agenda. Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go from here. Python. Python version 2.7.6 (an interpreter) Py2Exe version 0.6.9 win32-py2.7.exe PyCharm Community Edition (an IDE).

dougal
Télécharger la présentation

Learn IT 2014: 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. Learn IT 2014: Python Basic programming

  2. Agenda • Python, PyCharm CE • Basic programming concepts • Creating an executable file using Py2Exe • Where to go from here

  3. Python Python version 2.7.6 (an interpreter) Py2Exe version 0.6.9 win32-py2.7.exe PyCharm Community Edition (an IDE)

  4. Python • Easy to learn (simple syntax) • High-level scripting language • Available on PC, Mac, Unix and Linux systems • Drawing shapes using Turtle Module • Create web apps, games, or search engine (such as Google) • Lists of applications developed in Python https://wiki.python.org/moin/Applications

  5. Programming Basic Elements • Comments • Data Types, Variables and Variables Declaration • Functions • Turtle • Loops

  6. Comments • Are used to describe the meaning and purpose of your source code or program • Can be thought of as notes for ourselves and others • Are ignored by compilers and interpreters • Should be included in all programs

  7. Comments

  8. Programming Basic Elements • Comments • Data Types, Variables and Variables Declaration • Functions • Turtle • Loops

  9. Data Types • Common data types: • String – letters, numbers, and symbols • name = “Ros” • Numbers – integer and float • a = 1 # integer or int • b = 3.5 # float • Boolean – True or False • x = True • y = False • List – array in other languages • a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

  10. Variables & Variables Declaration • Used to store value or a piece of data • Declaring variables: • Should be something meaningful • Can start with alphabet, and underscore but not number • Case sensitive, i.e., name is not the same as Name

  11. Examples

  12. Comments and Variables Example

  13. Programming Basic Elements • Comments • Data Types, Variables and Variables Declaration • Functions • Turtle • Loops

  14. Function • A building block of code or a chunk of code that can be reused • To define a function in Python, we used a keyword “def” Example

  15. Function in Python

  16. Programming Basic Elements • Comments • Data Types, Variables and Variables Declaration • Functions • Turtle • Loops

  17. Turtle • Turtle (cursor) graphics is a Python graphic package for creating vector graphics (scalable graphics) • Turtle graphics module is included in Python • Three things we need to know: • Positions (distance) • Directions (angle) • States of the pens (line color, shape filled, line width, etc.)

  18. Directions in Turtle Facing Right Facing Left

  19. Degrees in Turtle 0 45 315 270 90 225 135 180

  20. Turtle Commands Visit https://docs.python.org/2/library/turtle.html for more information.

  21. Drawing Square

  22. Output

  23. Activity • Draw other shapes like triangle, rectangle, etc. • Formula

  24. Programming Basic Elements • Comments • Data Types, Variables and Variables Declaration • Functions • Turtle • Loops

  25. Loops • Looping means repeating the same thing over and over again. • Two kinds of loops: • For loop – repeat a certain number of times • While loop – repeat until a certain thing happens (or as long as some condition is met)

  26. For Loop • Explanations: • The variable i (loop index; loop counter) • The sequences in example started with the value 0 or 1 (example 1 and 2) and ended with any numbers (in example 1 and 2 the numbers were 4 and 2 respectively)

  27. For Loop • Range(start, stop, step) function is a short cut for entering starting and ending numbers • range() function returns the numbers between the start and stop • step in range() function is optional argument • An argument is a value that we put inside the parentheses and pass it to the function

  28. For Loop • Explanations: • range(1, 4) means our program will print out the words “Hello world” 3 times starting from 1 and ending before 4, which is 3. Note, step is optional. In this case we didn’t specify step so it will count by 1 • If you want to print out 5 Hello world, how would you do that using range(n1, n2)? • Example 2 showed another way of specifying a number of loops you want using just range(n) • range(2) in example 2 is the same thing as range(0, 1)

  29. For Loop Using “step” examples

  30. For Loop • We can also use a for loop to count something else other than numbers

  31. While Loop • While loop is used to repeat a block of codes an unknown number of times until a specific condition is met (conditional loop) • While loop is similar to if-else condition What happen if you leave out line 4? (Code from http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/whilestatements.html)

  32. Activity • Draw a square using a for loop • Formula

  33. Square Code

  34. Py2Exe Creating an executable file or standalone file using python script Py2exe, http://www.logix4u.net/component/content/article/27-tutorials/44-how-to-create-windows-executable-exe-from-python-script Mac user,http://pythonhosted.org/py2app

  35. Py2Exe • Create setup.py in the same folder where you want your python script to be executable • Example: #setup.py from distutils.core import setup import py2exe setup(console=[‘yourpythonfile.py'])

  36. Creating an EXE File • Go to Start and type “cmd” and press enter to open a command prompt Note: Visit, http://www.pythoncentral.io/py2exe-python-to-exe-introduction/ for more information

  37. Creating an EXE File Navigate to the folder that we want to create a python file into an executable file Example: 1. Create a directory called, “LearnIT2014” under C drive and copy the python file you want to create an executable file to that directory 2. In the command prompt window, type the following command to change the directory to LearnIT2014 c:\Users\n10rxa1> cd\ c:\> c:\>cd LearnIT2014 3. This will take you to c:\LearnIT2014 4. Type the following command after c:\LearnIT2014> c:\LearnIT2014>setup.py py2exe 5. When it is done creating, you will see two subfolders called “build” and “dist” created under LearnIT2014 6. Double click “dist” folder to open, you should see an exe file 7. The “dist” folder is the folder that you can pack and send it your friends and family

  38. Where to go from here • Python books: lists of free books about Pythonhttp://pythonbooks.revolunet.com/ • Hello World Bookhttp://www.manning.com/sande2/ • How to think like a computer scientisthttp://www.openbookproject.net/thinkcs/python/english2e/ • Python for Kidshttp://www.nostarch.com/pythonforkids

  39. Where to go from here • CodeAcademy: Python 2 http://www.codecademy.com/tracks/python • GrokLearning: Python 3https://groklearning.com/csedweek/ • Mobile app using Python http://kivy.org/ • Udemy (free Python on iPad) • Develop games with Pythonhttps://www.udemy.com/game-development-fundamentals-with-python/

  40. Happy Coding

More Related