Python 17 Mr. Husch.

Slides:



Advertisements
Similar presentations
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Advertisements

Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Title Center for Coastal and Ocean Mapping NOAA/UNH Joint Hydrographic Center Windows Python Tutorial Kurt Schwehr Jan 2007.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Chapter 2 Writing Simple Programs
Hello AP Computer Science!. What are some of the things that you have used computers for?
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
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.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Let’s get started!.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Chapter 2 Writing Simple Programs
JUST BASIC Lessons 6 – 9 Mr. Kalmes.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Python Lesson 12 Mr. Kalmes.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Introduction to Python
Formatting Output.
Python 14 Mr. Husch.
Variables, Expressions, and IO
Formatting Output.
Functions CIS 40 – Introduction to Programming in Python
Functions.
String Manipulation.
Python 18 Mr. Husch.
Writing Functions( ) (Part 5)
Computer Science and an introduction to Pascal
Procedures Programming Guides.
Python Lessons 13 & 14 Mr. Kalmes.
Coding Concepts (Sub- Programs)
Types, Truth, and Expressions (Part 2)
Comparing Strings – How to
Python 21 Mr. Husch.
Python 17 Mr. Husch.
Python programming exercise
Python 19 Mr. Husch.
Strings and the slice operator
Python Programming Language
CISC101 Reminders All assignments are now posted.
Python 16 Mr. Husch.
Introduction to Computer Science
Javascript Chapter 19 and 20 5/3/2019.
Types, Truth, and Expressions
Python 3 Mr. Husch.
Understanding Variables
Python Inputs Mr. Husch.
Python Lessons 13 & 14 Mr. Husch.
Python 16 Mr. Husch.
Python 19 Mr. Husch.
Python Lessons 7 & 8 Mr. Husch.
Python 10 Mr. Husch.
Python 18 Mr. Husch.
Python 12 Mr. Husch.
Python 4 and 5 Mr. Husch.
Python 8 Mr. Husch.
Basic 9 Mr. Husch.
Methods/Functions.
Class code for pythonroom.com cchsp2cs
CMPT 120 Lecture 4 – Unit 1 – Chatbots
 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.
Parameters and Arguments
Types, Truth, and Expressions (Part 2)
Week 7 - Friday CS 113.
Presentation transcript:

Python 17 Mr. Husch

Aim Students will use function in Python.

Function Functions can be created by using the word def. 1) Functions name pieces of code the way variables name strings and numbers. 2) They takes arguments the way your scripts take argv. 3) They let you make your own mini scripts and mini commands. Functions can be created by using the word def.

Create this program. lastname17

Explaining lines First we tell Python we want to make a function using def for "define". On the same line as def we then give the function a name, in this case we just called it "print_two" but it could be "peanuts" too. It doesn't matter, except that your function should have a short name that says what it does. Then we tell it we want *args (asterisk args) which is a lot like your argv parameter but for functions. This has to go inside () parenthesis to work. Then we end this line with a : colon, and start indenting. After the colon all the lines that are indented 4 spaces will become attached to this name, print_two. Our first indented line is one that unpacks the arguments the same as with your scripts.

Explaining the rest Now, the problem with print_two is that it's not the easiest way to make a function. In Python we can skip the whole unpacking args and just use the names we want right inside (). That's what print_two_again does. After that you have an example of how you make a function that takes one argument in print_one. Finally you have a function that has no arguments in print_none.

Running the program You should see what I have posted above, make sure when you run it you put in a random word after your 17 program so that it runs.

Building a Function Checklist 1) Did you start your function definition with def? 2) Does your function name have only characters and _ (underscore) characters? 3) Did you put an open parenthesis ( right after the function name? 4) Did you put your arguments after the parenthesis ( separated by commas? 5) Did you make each argument unique (meaning no duplicated names). 6) Did you put a close parenthesis and a colon ): after the arguments? 7) Did you indent all lines of code you want in the function 4 spaces? No more, no less. 8) Did you "end" your function by going back to writing with no indent?

Running a function checklist 1. Did you call/use/run this function by typing its name? 2. Did you put ( character after the name to run it? 3. Did you put the values you want into the parenthesis separated by commas? 4. Did you end the function call with a ) character

Use the checklists to create your program that you will be turning in. The program should have 4 functions in it, you can use what you started with as a working off point. All functions must work and print the arguments out at the end.