COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.

Slides:



Advertisements
Similar presentations
Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.
Advertisements

COMPSCI 101 Principles of Programming Lecture 6 – Getting user input, converting between types, generating random numbers.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Strings, if/else, return, user input
Data types and variables
Python Crash Course by Monica Sweat. Python Perspective is strongly typed, interpreted language is used to define scripts (Don't even need to define a.
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.
1.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Objectives You should be able to describe: Data Types
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
INTRODUCTION TO PYTHON. 1. The 5 operators in Python are: + - * / %
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Lecture 20 – Test revision COMPSCI 101 Principles of Programming.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
Input, Output, and Processing
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Chapter 3: Assignment, Formatting, and Interactive Input.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Topics – Programs Composed of Several Functions – Syntax Templates – Legal C++
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Input, Output and Variables GCSE Computer Science – Python.
CSC 222: Object-Oriented Programming Fall 2017
Line Continuation, Output Formatting, and Decision Structures
Line Continuation, Output Formatting, and Decision Structures
String and Lists Dr. José M. Reyes Álamo.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Topics Sequences Introduction to Lists List Slicing
Basic String Operations
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Topics Sequences Lists Copying Lists Processing Lists
Topics Basic String Operations String Slicing
Topics Sequences Introduction to Lists List Slicing
Introduction to Computer Science
Topics Basic String Operations String Slicing
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
More Basics of Python Common types of data we will work with
Topics Basic String Operations String Slicing
Presentation transcript:

COP 2510 Chapter 6 - Functions

Random function (randint) #import the desired function import random #integer random number in the range of 1 through 100. #(The values 1 and 100 are included in the range.) number = random.randint(1, 100) #note that the call to the random.randint function appears #on the right side of an = operator print number

Random function (randint) # This program displays five random # numbers in the range of 1 through 100. import random def main(): for count in range(5): # Get a random number. number = random.randint(1, 100) # Display the number. print number # Call the main function. main()

Random function (randrange) #assigns a random number in the range of 0 through 9 #to the number variable import random number = random.randrange(10) print number

Defining our own functions #Program 6‐2 (total_ages.py) # This program uses the return value of a function. def main(): # Get the user's age. first_age = input('Enter your age: ') # Get the user's best friend's age. second_age = input("Enter your best friend's age: ") # Get the sum of both ages. total = sum(first_age, second_age) # Display the total age. print 'Together you are', total, 'years old.' # The sum function accepts two numeric arguments and # returns the sum of those arguments. def sum(num1, num2): result = num1 + num2 return result # Call the main function. main()

Defining our own functions def is_even(number): # Determine whether number is even. If it is, # set status to true. Otherwise, set status to false if (number % 2) == 0: status = True else: status = False # Return the value of the status variable. return status value = input('Enter a number: ') if is_even(value): print 'The number is even.' else: print 'The number is odd.'

Math functions import math result = math.sqrt(16) print result radius = input("Enter radius: ") area = math.pi * radius**2 print area

Formatting Numbers myValue = print 'The value is %.2f' % myValue print 'The value is %.3f' % myValue print 'The value is %.1f' % myValue When the operand on the left side of the % symbol is a string, it becomes the string format operator The f that the format specifier indicates that we want to display a floating‐point number. The.2 that appears before the f indicates that the number should be rounded to two decimal places.

Formatting Multiple values value1 = value2 = print 'The values are %.1f and %.3f' % (value1, value2) print string % (number, number, …) In the print statement, the %.1f format specifier corresponds to the value1 variable and the %.3f format specifier corresponds to the value2 variable. When the code runs it will produce the following output: The values are 6.8 and 1.235

Formatting Integers dogs = 2 cats = 3 print 'We have %d dogs and %d cats.' % (dogs, cats)

String Functions #Getting a String's length #Use the len function to get the length of a string name = 'Charlemagne' strlen = len(name) print strlen #Appending Strings msg1='This is ' msg2 = 'one string.' message = msg1 + msg2 print message

String Functions #The upper and lower Methods littleName = 'herman' bigName = littleName.upper() print bigName bigName = 'HERMAN' littleName = bigName.lower() print littleName

String Functions #The find method string = 'Four score and seven years ago' position = string.find('seven') if position != -1: print 'The word "seven" was found at index', position else: print 'The word "seven" was not found.' This code will display: The word "seven" was found at index 15 (if we remove all the text between the quotes in the string variables, the code would display: The word "seven" was not found)

String Functions #Slicing full_name = 'Patty Lynn Smith' middle_name = full_name[6:10] print middle_name #How would you slice the last name? last_name = full_name[11:] print last_name #and the first name? first_name = full_name[:5] print first_name