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.

Slides:



Advertisements
Similar presentations
Getting Input in Python Assumes assignment statement, typecasts.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
Introduction to C Programming
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Python Programming Fundamentals
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Input, Output, and Processing
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Introduction to Programming with RAPTOR
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECET – Dynamic Programming with Python Spring 2013 Lecture L1 – Introduction to Python Page 1 Welcome! This is Professor Jai P. Agrawal. I will walk.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Python Let’s get started!.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Introduction to Programming
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Fundamentals of Programming I Overview of Programming
Chapter 9: Value-Returning Functions
Topics Introduction to Functions Defining and Calling a Void Function
Introduction to Python
Value-Returning Functions
Variables, Expressions, and IO
Review.
INPUT & OUTPUT scanf & printf.
Learning Outcomes –Lesson 4
Introduction to Value-Returning Functions: Generating Random Numbers
Terminal-Based Programs
12th Computer Science – Unit 5
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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 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()

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

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:

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. >>>

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.

CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 6 #exercise_jp1.py radius = 4 area = *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 = 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 = square meters. Save and Run in the module window. Write a module to calculate the area of a circle of radius 4 meters.

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 = 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 = *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 = square meters. One more time, Run this program in the module window. Enter a math expression through the keyboard,

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.

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()

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!!

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:

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.

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: >>> ================ RESTART ================================ >>> >>> import squareroot >>> squareroot.sqrt()

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.