Python 1 Introduction to Elementary Programming Classroom Course Manual

Python 1 Introduction to Elementary Programming Classroom Course Manual

Python 1 Introduction to Elementary Programming Classroom Course Manual Written, designed, and produced by: DoIT Software Training for Students Last Updated 1/16/2017 About Software Training for Students Software Training for Students is an organization on campus that provides free software training to all students and faculty. Our services include custom workshops, open-enrollment classes, one-on-one project help, and access to Lynda.com. For more information on the Software Training for Students (STS) program, visit our website at wisc.edu/sts. STS is part of the Division of Information Technology (DoIT) - Academic Technology at UW-Madison. For more information regarding DoIT Academic Technology, visit at.doit.wisc.edu. © University of Wisconsin Board of Regents. This manual and any accompanying files were developed for use by current students at the University of Wisconsin- Madison. The names of software products referred to in these materials are claimed as trademarks of their respective companies or trademark holder. If you are not a current member of the UW-Madison community and would like to use STS materials for self-study or to teach others, please contact [email protected]. Thank you. Topics Outline 1 Introduction 4 Creating Runnable Scripts 2 Simple Data Types 5 Functions 3 Sequence Data Types 6 Standard Library Introduction Welcome to Python! This course has been designed to give an introduction to the Python programming language. Python is a dynamic programming language that is used in a variety of application domains. Python is based on object-oriented programming, similar to the likes of C++, Java, C#, Perl, Ruby, etc. This manual will include explanatory text along with code examples that you should be typing to follow along. All code is placed within snippets applicable to the language being used. This manual is based on Python 3.5, and is quite different from Python 2.7. The code snippets and exercises are only applicable to any 3.x version of Python. Python 2.7 is the legacy version and has not been updated since 2010. Python 3 is the present and future of the language and is a better starting point for beginners, with wider applications. Running Python If not already installed, Python can be downloaded from the following website: https://www.python.org/downloads/. After a straightforward installation process, find and run the IDLE (Python 3.5) application. This should open up the Python interpreter shell which allows the user to enter code line by line for testing or trial and error. The interpreter provides many options including being able to open a new script window where complete Python programs can be written in a built in Integrated Development Environment (IDE). Macs have Python built-in and can be found via the Terminal. Search for and open the Terminal via the applications menu or Spotlight search. In the terminal, type in python. Note that this will be version 2.7. It is recommended that you use version 3.4 and above for this course. Please note that, on the DoIT computers, only the Windows partition has Python 3 installed. Getting help Python has a very detailed help menu that can be entered through the Python interpreter. Once you have the Python Interpreter opened, type in the following: #helpMenu >>> help() This will enter you into the Python help utility. You can enter any module, keyword or topic into this utility to get more information. For example, to get more information about strings you would type str inside of the help module. This will bring up the help menu for strings specifically. To return to the interpreter, use quit. #helpMenuModule >>> help() help> str #help on strings... help> quit #quit help menu Another approach to get help on certain modules in Python without the help menu is to insert the module names as an argument when in the interpreter. We do not need to quit from this as it is displayed in the interpreter. >>> help(str) #help on strings... If the built-in resources do not suffice, additional help can be found over at Lynda.com in the form of video tutorials. The Python website also offers support over at https://www.python.org/about/help/ Simple Data Types As in other programming languages, variables are symbols or names that stand for a specific value. Variables can be many different types in Python: numbers, strings, lists, tuples and dictionaries. There are some naming conventions that need to be followed when naming a Python variable. Variable names cannot begin with a number or contain punctuation characters such as @, $, and %, with the exception of the underscore character. Python keywords cannot be used as variable names either. A list of such keywords is provided in the code snippet below. #some incorrect variable syntax >>> 313ph4nt = 349 #variable name begins with number >>> s.t.s = 420 #contains special characters except underscore >>> if = 'Weaponizing Didgeridoos by 2024' #"if" is Python keyword #a list of some keywords that cannot be used as variable names and del from not while as elif global or with assert else pass yield is break except import print exec in raise try continue finally return for lambda class def Python is a case sensitive language, so two variables named Buckybadger and buckybadger would be considered different variables. Most programmers follow the camelCase naming convention where the first letter of each word in the name is capitalized except for the first word. Variables may contain numbers but must start with either a letter or an underscore. Below is an example that explains the case sensitivity of Python. #caseSensitivity >>> buckyBadger = 5 >>> buckybadger = 10 >>> buckybadger + buckyBadger 15 Variables follow this structure: variableName = value. The text entered after the equals sign depends on the type of variable you are using. To demonstrate, we are going to start by creating some string variables. Strings Strings in Python are unique because they are immutable. This means that the string cannot be modified directly. Many mutable data structures provide methods to allow you to dynamically change values of a given variable or data structure. With immutable data types, the developer must create a new instance of the data type (in this case a string) and assign the variable to the new data structure. This may sound complicated, but in practice it is very easy to create new strings by combining other strings. Simply remember that string methods will not directly modify the contents of a variable. Strings are created by placing text in between either single or double quotes. We will now use the print keyword to display text. Try out the snippets below. #displayText >>> print("Hello World!") Hello World! Congratulations! You have displayed your first string in Python. Strings in Python are easily combined and these combinations can be modified using the comma (,) and plus (+) operators. Type the following in the Python interpreter and press return after each: #stringCombine >>> print("Hello","World!") Hello World! >>> print("Hello"+"World!") HelloWorld! The comma operator will put a space between the strings and the plus operator will not include a space when the strings are combined. Text can also be assigned to variables which makes manipulation of the string easier. #stringVariable >>> s = "Hello World!" #assign text to variable named s >>> print(s) #display variable s Hello World! String Methods Python has a series of built-in methods that allow users to return manipulated string values. There are many more methods built into Python that are used on other data types. A method is considered a subroutine or a procedure that is associated with a given type and performs its function when called. In many other languages these are referred to as member functions or simply functions. You have already defined the string s which we will be applying the string methods to. Perform the snippets below. #stringMethods1 >>> print(s.capitalize()) Hello world! >>> print(s.upper()) HELLO WORLD! >>> print(s) Hello World! Since the string s is immutable, that means the string methods are not making changes to s. They are simply returning modified values of the string. Let's say that we want to modify the values of s so that all the letters in the string are uppercase. The snippet below does this. #stringMethods2 >>> s = s.upper() >>> print(s) HELLO WORLD! Being able to search for information or data through string values can be a very useful procedure in Python and other programming languages. The str.find(value) method will return a positive value if the string contains the value being searched for; otherwise the method will return -1. The positive number returned if the value is found is the index number at which the first character was found, which is explained in the next section. The value or argument within the find method is case sensitive. Test it out below. #stringFind >>> s.find("ell") -1 >>> s.find("ELL") 1 Another useful method is replace. This method will take two string values as inputs. It will find the first value, if it exists, and return the string with the second value in place of the first value. The replace method is also case sensitive like the find method. If the first value does not exist in the string, the replace method will just return the original string. Type out the following code in the Python interpreter to demonstrate. #stringReplace >>> print(s.replace("world","EARTH")) 'HELLO WORLD!' >>> print(s.replace("WORLD","EARTH")) 'HELLO EARTH!' The first line of code just returns the original value of the string because the replace method is case sensitive. The returned values are not permanent; the string s would need to be redefined as done previously to retain any of the changes from the replace method.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    23 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us