Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.

Similar presentations


Presentation on theme: "CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome."— Presentation transcript:

1 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome to Python 2.7! This is the online help utility. If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://docs.python.org/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, or topics, type "modules", "keywords", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose summaries contain a given word such as "spam", type "modules spam". help> >>> help()

2 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 2

3 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 3 Help on built-in module math: NAME math FILE (built-in) DESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(...) acos(x) Return the arc cosine (measured in radians) of x. … help> math Let us see what functions are available in math module:

4 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 4 Help on built-in function exp in math: math.exp = exp(...) exp(x) Return e raised to the power of x. help> math.exp Let us see what the exp method in the math module does: We must write the module name followed by dot (.) and the name of the method. To get back the normal python prompt >>>, type the quit after the help> help> quit You are now leaving help and returning to the Python interpreter. If you want to ask for help on a particular object directly from the interpreter, you can type "help(object)". Executing "help('string')" has the same effect as typing a particular string at the help> prompt. >>>

5 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 5 >>> help(math) Traceback (most recent call last): File " ", line 1, in help(math) NameError: name 'math' is not defined Will return the following error message: Help on built-in module math: NAME math... >>> help("math") >>> help('math.exp') Help on built-in function exp in math: math.exp = exp(...) exp(x) Return e raised to the power of x.

6 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 6 #exercise_jp1.py radius = 4 area = 3.1415*radius**2 print "the area of circle of radius", radius, “ meters is = ", area, “square meters.” >>> ============== RESTART ================================ >>> the area of circle of radius 4 meters is = 50.264 square meters. Alternate way of running a module is to save it as a module and then call it from the Interpreter window as shown below: >>> import exercise_jp1 the area of circle of radius 4 meters is = 50.264 square meters. Save and Run in the module window. Write a module to calculate the area of a circle of radius 4 meters.

7 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 7 >>> ================== RESTART ================================ >>> Specify the radius in meters and then enter: 5 the area of circle of radius = 5 meters is = 78.5375 square meters. To make the experience interactive, we will now ask the user to enter the value of radius. Modify the Python module as shown below: #exercise_jp1b.py radius=input("Specify the radius in meters and then enter: ") area = 3.1415*radius**2 print "the area of circle of radius = ", radius, " meters is = ", area, "square meters." Input is a built in function to get the input from the keyboard. The input from the keyboard is evaluated and the assigned to the variable radius. The input is basically a delayed input. Save and Run in the module window. The result in the interpreter window is Specify the radius in meters and then enter: 2**3 the area of circle of radius = 8 meters is = 201.056 square meters. One more time, Run this program in the module window. Enter a math expression through the keyboard,

8 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 8 >>> def greetings(): print "Hello friend" print "How are you today!!" >>> greetings() The function name is preceded by def, followed by ( ) :. The function name greetings has no arguments to pass, therefore, nothing is entered within parentheses. The parentheses are important though. The semicolon after parentheses is indication that more statements are to follow on the same line or the next line(s). A new line is entered by pressing the. Python does not prompt on the next line but expects the programmer to enter statements. Note that Python creates an indent on the next line(s) to follow. A standard indent is exactly 4-space wide. After entering all statements, the execution will start only after the programmer enters a blank line (obtained by hitting the key twice) lets Python know that the definition is finished, and the interpreter responds with another prompt. The result is shown in the yellow background. Hello friend How are you today!! We will now write a function for greetings in the shell mode.

9 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 9 #Greetings.py#module name def greetings(): print "Hello friend“#the indent is 4-space wide print "How are you today!!“ Let us write this function in a script mode in a module: A function definition ends when it encounters a blank line or a new line without indent. Save the above file as greetings.py, Run in the module window and then invoke the function (call) in the interpreter window on the prompt. Hello friend How are you yes today!! >>> greetings()

10 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 10 Let us add the name of person in the greeting function. Modify the script file as the following, Save, and Run and then invoke from the interpreter window as shown: #Greetings.py#module name def greetings(person):#the function header print "Hello", person print "How are you today!!" >>> greetings ("John") Hello John How are you today!! Person is passed as the argument within the ( ) in the function header. Let us add the name of person in the greeting function. Modify the script file as the following, Save, and Run and then invoke from the interpreter window as shown: Alternately, Save in the module window and then invoke it from the interpreter window as below: >>> import Greetings#import the module that contains the function >>>Greetings.greetings (‘John’)#invoke by the module name.function name(..) Hello John How are you today!!

11 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 11 A Welcome Program Write a program to ask name, age and address from an applicant for a job in a company and then greet him/her. Please tell your name then enter: Ram Please give your age then enter: 3 Please give your address then enter: Katy, Texas Welcome Ram from Katy, Texas #welcome_xx.py def welcome(): name = raw_input(“Please tell your name then enter: ") # the raw_input takes in whatever is entered through the keyboard as is age = raw_input(“Please give your age then enter: ") address = raw_input(“Please give your address then enter: ") print "Welcome ", name, " from ", address >>> welcome() Save and Run in the module window. Then invoke the function welcome() in the interpreter window:

12 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 12 Now we will do a project using Python: Calculate and print the squareroot of numbers from 0 to a user supplied integer number. Open Python IDLE, Open a new module window and type in the following script: #squareroot.py """Written by: your name Date September 12, 2012 Project: Calculate and print the square root of numbers from 0 to a user supplied integer number. ””” def sqrt(): print "This program calculates the square root of numbers from 0 to a user \ Supplied integer number." x = input("Enter a number between 1 and 10: ") i = 0 while i <= x: print i, " ", i ** (1.0/2) i = i+1 Note that we can break a line at will inside a comment enclosed within three double quotes or a comment that starts with a # at the beginning of a line. However, the print line can be broken (or continued on the next line) only by adding a \ at the end of line.

13 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 13 This program calculates the squareroot of numbers from 0 to a user supplied integer number. Enter a number between 1 and 10: 8 0 0.0 1 1.0 2 1.41421356237 3 1.73205080757 4 2.0 5 2.2360679775 6 2.44948974278 7 2.64575131106 8 2.82842712475 >>> ================ RESTART ================================ >>> >>> import squareroot >>> squareroot.sqrt()

14 CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 14 Homework 1: Due in the Next session. Give the python programs and results for the following program: 1.Modify the squareroot program to calculate and print the squareroot and the third root.


Download ppt "CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome."

Similar presentations


Ads by Google