CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.

Slides:



Advertisements
Similar presentations
Adapted from John Zelle’s Book Slides
Advertisements

Python Programming: An Introduction to Computer Science
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Introduction to C Programming
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Structured programming
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Introduction to Python
Chapter 2 Writing Simple Programs
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
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.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Adapted from John Zelle’s Book Slides1 CS177 Python Programming Chapter 1 Computers and Programs.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Python Programming Fundamentals
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
Introduction to Python
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Python Programming: An Introduction to Computer Science
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
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.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
If statements while loop for loop
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
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.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
A Python Tour: Just a Brief Introduction "The only way to learn a new programming language is by writing programs in it." -- B. Kernighan and D. Ritchie.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
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.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 1 Computers and Programs.
Python Programming Module 1 Computers and Programs Python Programming, 2/e1.
And now for something completely different…
Chapter 2 Writing Simple Programs
A Python Tour: Just a Brief Introduction
Python Let’s get started!.
Variables, Expressions, and IO
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
CS190/295 Programming in Python for Life Sciences: Lecture 1
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
15-110: Principles of Computing
CISC101 Reminders All assignments are now posted.
Python Basics with Jupyter Notebook
COMPUTER PROGRAMMING SKILLS
Chopin’s Nocturne.
Python While Loops.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1

The building blocks of Python  keywords: 'False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else‘, etc.  Built-in types: numeric (integer, floating point)), sequences (strings), etc.  Built-in functions: print(), format(), int(), input(), etc.  Libraries of modules (math, sys, etc.) CSC 110 B 2

The Magic of Python When you start Python, you will see something like: Python (default, May , 23:07:10) [MSC v bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> CSC 110 B 3

The Magic of Python  The “>>>” is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements.  >>> print("Hello, world“) Hello, world >>> print(2+3) 5 >>> print("2+3=", 2+3) 2+3= 5 >>> CSC 110 B 4

The Magic of Python  Usually we want to execute several statements together that solve a common problem. One way to do this is to use a function.  >>> def hello(): print("Hello") print("Computers are Fun") >>> CSC 110 B 5

The Magic of Python  >>> def hello(): print("Hello") print("Computers are Fun") >>>  The first line tells Python we are defining a new function called hello.  The following lines are indented to show that they are part of the hello function.  The blank line (hit enter twice) lets Python know the definition is finished. CSC 110 B 6

The Magic of Python  >>> def hello(): print("Hello") print("Computers are Fun") >>>  Notice that nothing has happened yet! We’ve defined the function, but we haven’t told Python to perform the function!  A function is invoked by typing its name.  >>> hello() Hello Computers are Fun >>> CSC 110 B 7

Naming conventions  Function, variable, parameter  Use letters, digits and underscore  Can’t start with a digit  Different styles: myFunction, aVariable (mixed case) my_function, a_variable (all lowercase) CSC 110 B 8

The Magic of Python  What’s the deal with the ()’s?  Commands can have changeable parts called parameters that are placed between the ()’s.  >>> def greet(person): print("Hello",person) print ("How are you?") >>> CSC 110 B 9

The Magic of Python  >>> greet("Terry") Hello Terry How are you? >>> greet("Paula") Hello Paula How are you? >>>  When we use parameters, we can customize the output of our function. CSC 110 B 10

The Magic of Python  When we exit the Python prompt, the functions we’ve defined cease to exist!  Programs are usually composed of functions, modules, or scripts that are saved on disk so that they can be used again and again.  A module file is a text file created in text editing software (saved as “plain text”) that contains function definitions.  A programming environment is designed to help programmers write programs and usually includes automatic indenting, highlighting, etc. CSC 110 B 11

An example of a module # File: salutations.py # A simple program illustrating the use of functions def greet(person): print('Hello ' + person + '!') print('How are you?') def part(person): print('Nice meeting you, ' + person) print('Have a good day!') greet('Sara') part('Sara')  We’ll use filename.py when we save our work to indicate it’s a Python program.  In this code we’re defining two functions called greet and part.  The code at the end (greet(‘Sara’)…) is executed when the module is run. CSC 110 B 12

Inside a Python Program # File: salutations.py # A simple program illustrating the use of functions  Lines that start with # are called comments  Intended for human readers and ignored by Python  Python skips text from # to end of line CSC 110 B 13

Another example # File: chaos.py # A simple program illustrating chaotic behavior def main(): print("This program illustrates a chaotic function") x = eval(input("Enter a number between 0 and 1: ")) for i in range(10): x = 3.9 * x * (1 - x) print(x) main()  In this code we’re defining a new function called main.  The main() at the end tells Python to run the code. CSC 110 B 14

The Magic of Python >>> This program illustrates a chaotic function Enter a number between 0 and 1: >>> CSC 110 B 15

Inside a Python Program def main():  Beginning of the definition of a function called main  Since our program has only this one function, it could have been written without the main function.  The use of main is customary, however. CSC 110 B 16

Inside a Python Program print("This program illustrates a chaotic function")  This line causes Python to print a message introducing the program. CSC 110 B 17

Inside a Python Program x = eval(input("Enter a number between 0 and 1: "))  x is an example of a variable  A variable is used to assign a name to a value so that we can refer to it later.  The quoted information is displayed, and the number typed in response is stored in x. CSC 110 B 18

Inside a Python Program for i in range(10):  For is a loop construct  A loop tells Python to repeat the same thing over and over.  In this example, the following code will be repeated 10 times. CSC 110 B 19

Inside a Python Program x = 3.9 * x * (1 - x) print(x)  These lines are the body of the loop.  The body of the loop is what gets repeated each time through the loop.  The body of the loop is identified through indentation.  The effect of the loop is the same as repeating this two lines 10 times! CSC 110 B 20

Inside a Python Program for i in range(10): x = 3.9 * x * (1 - x) print(x)  These are equivalent! x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) x = 3.9 * x * (1 - x) print(x) CSC 110 B 21

Inside a Python Program x = 3.9 * x * (1 - x)  This is called an assignment statement  The part on the right-hand side (RHS) of the “=“ is a mathematical expression.  * is used to indicate multiplication  Once the value on the RHS is computed, it is stored back into (assigned) into x CSC 110 B 22

Inside a Python Program main()  This last line tells Python to execute the code in the function main CSC 110 B 23

Chaos and Computers  The chaos.py program: def main(): print("This program illustrates a chaotic function") x = eval(input("Enter a number between 0 and 1: ")) for i in range(10): x = 3.9 * x * (1 - x) print(x) main()  For any given input, returns 10 seemingly random numbers between 0 and 1  It appears that the value of x is chaotic CSC 110 B 24