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.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Python Programming: An Introduction to Computer Science
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Python November 14, Unit 7. Python Hello world, in class.
Chapter 2 Writing Simple Programs
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
Python.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Python Programming Fundamentals
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
General Programming Introduction to Computing Science and Programming I.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
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.
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.
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!
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.
 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)
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
GCSE Computing: Programming GCSE Programming Remembering Python.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
A Sample Program #include using namespace std; int main(void) { cout
Getting Started With Python Brendan Routledge
Introducing Python 3 Introduction to Python. Introduction to Python L1 Introducing Python 3 Learning Objectives Know what Python is and some of the applications.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
Computer Concepts 2016 Unit E Step-by-Step: Programming with Python ENHANCED EDITION.
Chapter 2 Writing Simple Programs
CST 1101 Problem Solving Using Computers
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
Python: Experiencing IDLE, writing simple programs
Python Let’s get started!.
Introduction to Python
Pamela Moore & Zenia Bahorski
Variables, Expressions, and IO
Intro to PHP & Variables
Unit E Step-by-Step: Programming with Python
Python programming exercise
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
General Computer Science for Engineers CISC 106 Lecture 03
Presentation transcript:

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 will execute

What is Python? Python is a high level language that we (humans) can understand – Other examples include C++ and Java A machine can only understand low level language So we process the high level to low level so the computer will understand!

Translation How do we translate high level languages to low level languages? – We use interpreters and compilers! – Interpreters process the program a little bit at a time and runs it – Compilers translate everything before running it

Some Python Vocab Program – a file of code that may contain functions Script – a short code that we can run in a command line Variable – names we assign the values to, allowing us to reuse them later on – For example: x = 1 or msg = “Hello world!” – Variables can be changed throughout a program – For example: x = 1, x = x + 1

Python Vocab Comments – notes ignored by the computer – For example: x + y # variables store user input Operators – mathematical symbols – +, -, *, /, ** (exponents), == (equality)

Python Vocab Keyword – words with meaning/purpose in Python – For example: “and”, “print”, “if” Expression – statements that produce values – 3 + 5, “Hello world!” Error – program has a problem in the command area Instance – one run-through of a program

Indentation A REQUIREMENT IN PYTHON! Indenting specifies the “scope” of different chunks of your code – Everything indented after a first, unindented line “belongs” to that line!

Things to Note Python is case sensitive – A function called “first” is different than a function called “FIRST” or “First” or “fiRSt” Python doesn’t like spaces or punctuation marks – You can’t name your function “spam Five” or “spam.Five” – You could, however, name your function“spamFive” or “spam_Five” Some words in Python can’t be used as names – Keywords can never be used as function/variable names – Check the colors of the words! Purple and orange are KEYWORDS!

LET’S PLAY WITH PYTHON!

Our age(number) function We had an argument passed into our function! The argument is known as a parameter Example def add(a, b): print(“This is a + b: “, a+b) a and b are the parameters

Data Types Numeric – Integers (5, 2, -1) – Floating Point Numbers (0.2, , 28.92) Non-numeric – String (text), lists, dictionaries, etc – Basically anything you can’t add up using a simple plus sign (+)

Not a String? Not a Problem! You can format outputting variables you’ve already defined x = 42 print “The value of x is”, x, “.” What does this print out?

Not a String? Not a Problem! The output is – The value of x is 42. The bottom will cause an error. x = 42 print “$” + x We can’t combine string and numbers. So what do we do?

Not a String? Not a Problem! We can make our numerical variable a string! x = 42 print “$” + str(x) This will print out $42

More on Variables Variables can hold all kind of values, including strings, numbers, and user input To assign a string value to a variable, you have to wrap the string in quotes firstName = “Jessi” lastName = “Cheung” mathProblem = “5 + 5” print lastName, “,”, firstName, “;”, mathProblem What will this print?

More on Variables The output is: Cheung, Jessi; Variables can also be assigned new values that are relative to their old values total = 10 print “Original total:”, total total = total + 4 print “New total:”, total What does this print?

More on Variables The output is Original total: 10 New total: 14 Remember: a variable has to be defined on a previous line before it can be used on the right-hand side of an equation ABC = ABC + 4 print “ABC:”, ABC ERROR. There was no mention of the value of “ABC” before the line trying to redefine it.

Python Arithmetic Try typing the following code in your program area and see what comes out! def main(): a = 12 b = 2 c = 16 d = 3 e = 2.5 print “The value of a is”, a print (a / b) * 5 print a + b * d print (a + b) * d print b ** d print c – e a = a + b print “The value of a is”, a

Python Arithmetic Is this what you got? the value of a is the value of a is 14

Exercise time! Write a program that takes in a Celsius temperature (celsius) and returns the temperature in Fahrenheit – Hint: To get Fahrenheit, multiply the Celsius by (9.0/5.0) and add 32

Taking User Input Sometimes, instead of passing in an argument as a parameter, we can have the computer ask us what we want!

Taking User Input name = requestString("Enter your name:") print name first pops up a dialog box (where you can enter a name, say ‘John Doe’): then outputs John Doe

Taking User Input Let’s try it with numbers! def requestNumber(): num = input(“Enter a number:”) print “Your number is:”, num print “Your number squared:”, num*num

Taking User Input What if you tried inserting a string into… def requestNumber(): num = input(“Enter a number:”) print “Your number is:”, num If you type hello, there will be an error. If you type “hello”, it will work This is where raw_input comes into play!

Taking User Input raw_input will take exactly what you type and make it into a string def requestName(): name = raw_input(“Enter your name:”) print name, “is awesome!” Try typing in a number!

Let’s Write a Program! Let’s write a program that will calculate the area and the circumference of a circle! Open a new window (File/New Window)! At the top of your (blank) file, write the following: # file name: circle.py # author: Jessi Cheung # description: a program to calculate the area and the circumference of a circle Save the program as circle.py

Let’s Write a Program Let’s define this program as main. def main(): Your turn! – Use input to ask the user for the radius!

Let’s Write a Program We now have (besides our heading) def main(): radius = input(“What is the radius? “) Now let’s start the calculations! – Circumference of a circle: Pi (3.14) times (radius times two) – Area of a circle: Pi (3.14) times (radius squared)

Let’s Write a Program! We now have (besides our heading) def main(): radius = input(“What is the radius? “) circumference = 3.14 * (2 * radius) area = 3.14 * (radius * radius)

Let’s Write a Program! Now for the finishing touches! Let’s print out the output so we can see it!

Let’s Write a Program def main(): radius = input(“What is the radius? “) circumference = 3.14 * (2 * radius) area = 3.14 * (radius * radius) print “The radius of our circle is”, radius print “The circumference of our circle is”, circumference print “The area of our circle is”, area

Let’s RUN the Program! Once you save your program, press F5 on your keyboard Nothing happens?!

Let’s RUN the Program! You must call your program! Call using main()

Let’s RUN the Program Another way you could run the program…

For Loops Also known as the “definite loop” – we know exactly how many times the loop will happen! Allows you to specify a list of items (numbers, words, letters, etc.) and specify actions to be performed on each one The official syntax for the for loop is: for in :

Help the Kittens! You are working at an animal shelter, and you’re asked to take a group of kittens and bathe, dry, and feed each one individually

Use a Loop! Using a for-loop type notation, your instructions would look like this: Kittens = [kitty #1, kitty #2, kitty #3,...] for kitty in Kittens: bathe kitty dry kitty feed kitty

Basic Loop See what happens when you put in this: phrase = “Hello world!” for letter in phrase: print “the next letter is:”, letter

Basic Loop The output! the next letter is: H the next letter is: e the next letter is: l the next letter is: o the next letter is: the next letter is: w the next letter is: o the next letter is: r the next letter is: l the next letter is: d the next letter is: !

What Just Happened? Python went through the string one character at a time, treating the string like a sequence That means that the string can be split into its components (the characters)

Accumulator Variables When you’re using a for loop, sometimes you might want to keep a running total of numbers you’re calculating, or recombine bits of a string

Accumulator Variables Steps: 1.Define a variable for the first time before the loop starts 2.Redefine it as itself plus some operation in the body of the for loop total = 0 for num in [1,2,4,10,20]: total = total + num print “Total:”, total This will give the output Total: 37

Accumulator Variables What is the point of accumulator variables? – Counting – Keeping score – Debugging

Conditional Statements Equals: == Does not equal: != Try this: x = 1 if (x != 2): print “Artemis rocks”

Want to learn more? Go to: wiki.python.org/moin/BeginnersGuide