Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

Programming in Visual Basic
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
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.
Recitation 1 Programming for Engineers in Python.
Python.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
An Introduction to Textual Programming
Introduction to Python
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
2440: 211 Interactive Web Programming Expressions & Operators.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
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)
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
Python Conditionals chapter 5
Decision Structures, String Comparison, Nested Structures
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
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.
Python Let’s get started!.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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.
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 Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
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.
Input, Output and Variables GCSE Computer Science – Python.
Few More Math Operators
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Introduction to C++ Programming
ECS10 10/10
Variables, Expressions, and IO
Engineering Innovation Center
Decision Structures, String Comparison, Nested Structures
Introduction to Java, and DrJava part 1
CS 100: Roadmap to Computing
A look at Python Programming Language 2018.
Python programming exercise
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
CS 100: Roadmap to Computing
Introduction to Python
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Python Lesson Week 01

What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while Boolean logic Integers Create Python files

Install Python Go to Download - Python – (Release Date ) Windows x86 MSI installer (32 bit) Windows x86 MSI installer (64 bit) Create a folder C:\Projects to store your projects files. Create a sub folder week01 Launch IDLE

Python Shell – Your first Python commands Open IDLE At the “>>>” type print(“Hello World”) Press What do you see? Try typing this with other words

It would be great to not have to type so much + P Will copy down the previous line Then you can edit it This can work to copy down even earlier commands

Let’s play with Strings A string is a sequence of characters. print your name print “yesyesyesyes” print “MaybeMaybeMaybe”

Easier ways to repeat yourself print(“yesyesyesyes”) print(“MaybeMaybeMaybe”) But take a look at this: print(“yes” * 4) print(“Maybe” * 3)

String Concatenation You can use + to combine strings. For example: print(“string1”+ “string2”)

Easier ways to repeat yourself print(“MaybeMaybeMaybeYesYesYesYes”) Could be as easy as: print(“Maybe” * 3 + “Yes” * 4)

Delimiting Strings You can use single quotes or double quotes. But whatever you start with you need to end with. For example these are valid: print(“I am a String”) print(‘I am a String’) This is valid too: print(“I ‘m a String”) print(‘Some people say, “I am a String”’) Don’t forget to save time with + P

Escape characters Sometimes you need to say this character means something different than usual. In Python this is done with the backslash “\” For example, sometimes you want to use the delimiter as an ordinary character. print(‘I \‘m a String’) print(“Some people say, \“I am a String\””)

Other escape characters \n- newline \t- tab print(“I am line 1\nI am line2”) print(“Name\tAge\nNathan\t39”)

Using Variables firstName = “Nathan” lastName = “Price” print(firstName) print(lastName) Variable Name - Any meaningful combination of characters; The characters must all be letters, digits, or underscores _, and must start with a letter. In particular, punctuation and blanks are not allowed. Important to remember : Python is case sensitive: The identifiers last, LAST, and LaSt are all different. Good Practice: Use camelCase when naming a variable with multiple words – example: interestRate You are not allowed to use following keywords as variable names

Getting input from the user userName = input(“What is your name? ”) print(“Welcome “ + userName) *Change it so the user enters their name on a blank line.

Let Us Start Writing Programs Create a File->New print(“Something”) File->Save as “programname.py” in folder week01 Run first program – Run Module (F5) Keyboard shortcuts Ctrl+N as File->New Ctrl+S as File->Save Ctrl+Z as Edit->Undo F5 – Run Module

Finding the length of strings len() can be used to find the length of strings len(“Hello”) 5 len(“12345”) ??

Exercise Create a new file called “NameLength.py” Write a program that asks the user and stores it in a variable Tell the user how long their name is

String Functions – fun with case string variables and literals have functions you can use on them. Let’s take a look: print(“hello”.upper()) firstName = input(“What is your first name?\n”) print(“Did you say,\”” + firstName.upper() + “\”?”)

Try these and tell me what they do? lower() capitalize() swapcase() title() See more string functions at:

Exercise Write a program that asks for a person’s full name And responds back with “Welcome Firstname Lastname” with the case correct. For example, if I type “nathan price” it would say “Welcome Nathan Price”

Working with parts of strings You can just get one character from a string by using “[]”. For example, if I want the first character I could type in print(“This is a string”[0]) This would print “T” Now try to print the 4 th letter

The 2 nd and 4 th character in your name Write a program that asks for the users name Makes it all upper case And then says, “The second character is ‘A’ and the fourth character is ‘H’)

You can count the other way What is printed when you type: print(“Something”[-1])

Exercise Take your previous program and add a new statement “The last character in your name is ‘N’”

“Assignment” vs. “Comparison” One equal always means “assign”. It makes the two things equal myName = “Nathan” Now “myName” is the same as “Nathan” Two equals is asking the question “are these equal?” and the computer will decide if it is “True” or “False” myName == “Nathan” Asks the question, “Is myName the same as ‘Nathan’?” and will return either True or False

“if/else” statement ***Indention is very important in Python*** Create a new file (NameCheck.py) and type this program exactly: name = input(“What is your name?\n”) if name == “Nathan” : print(“Welcome. You may enter.”) else: print(“Access Denied!!”)

Exercises Create a program that asks people what state they live in. If “Texas” then tell them that they “Qualify” otherwise tell them “You need to move to Texas” Create a program that has the user type in a sentence, if the last character is a “?”, the say “Why do you ask?” otherwise it should say, “That’s interesting.”

Combining conditions with “and” sentence = input(“Type in a sentence.\n”) if sentence[0] == “A” and sentence[-1] == “.”: print(“I have heard that before.”) else: print(“That’s new.”) What happens if I enter: A bear walks into a bar. How about: A bear walks into a bar?

“while” statement In addition to “if/else” you can create loops using “while” Try this: stop = “” while not stop == “yes”: stop = input(“Do you want me to stop? (type ‘yes’)\n”)

What is the final answer? Rule: Parenthesis are evaluated first not is evaluated next; and is evaluated next; or is evaluated last. False or not True and True

What is the final answer? Rule: Parenthesis are evaluated first not is evaluated next; and is evaluated next; or is evaluated last. False or not True and True False or False and True False or False False

What is the final answer? Rule: Parenthesis are evaluated first not is evaluated next; and is evaluated next; or is evaluated last. False and not True or True

What is the final answer? Rule: Parenthesis are evaluated first not is evaluated next; and is evaluated next; or is evaluated last. False and not True or True False and False or True False or True True

What is the final answer? Rule: Parenthesis are evaluated first not is evaluated next; and is evaluated next; or is evaluated last. True and not (False or False)

What is the final answer? Rule: Parenthesis are evaluated first not is evaluated next; and is evaluated next; or is evaluated last. True and not (False or False) True and not False True and True True

Exercise Evaluate 1.True and True 2.True and False 3.False and True 4.False and False 5.Not False 6.Not True 7.Not False and Not True Evaluate 1.True or True 2.True or False 3.False or True 4.False or False

Integer Variables count = 0 print(count) count = count + 1 print(count)

Exercise Write a program that prints the count from 0 to

+= Adding an amount to a variable is such a common practice that there is a short cut. These two do the same thing: count = count + 1 count += 1 Rewrite your previous program to use +=

Exercise Modify the following program to say how many times the person has been asked: stop = “” while not stop == “yes”: stop = input(“Do you want me to stop? (type ‘yes’)\n”)

Exercise Write a program that asks a person to type in a sentence and then it prints out every other letter.

Homework for the week: Create a “MadLibs” program Create a program that asks the person their name, and then asks them to type in one letter at a time. If they miss any letter, then say “Whoops!!”. But if they get them all then say, “Wow! You win!” (You will need to use a variable to store an integer containing the length of the name, and then you will need to have another integer starting at zero and then increment until you get to 1

Save for the next lesson

Exercise 3 Evaluate 1.-(-(-(-2))) == -2 and 4 >= 16** % 4 != 300 / 10 / 10 and False 3.-(1**2) < 2**0 and 10 % 10 <= * 2 Evaluate 1.100**0.5 >= 50 or False 2.2**3 == 108 % 100 or 'Cleese' == ‘Cheddar'

str() [Alphanumeric] String Formatting with %

More data types Number data type Int float Boolean data type Determine data type of a variable – type() Operator & Expression Arithmetic Expression - basic math ops, mod, exponent, floor division, parenthesis Comparison Operator – (==, !=, >, =, <=) Assignment Operator – (=, += etc.) Boolean Operator – and, or, not

Exercise Calculate Simple Interest Calculate Compound Interest Convert a temperature from Fahrenheit to Celsius

Exercise -2 Express Is 17 less than 4 ? Is 3 greater than equal to 1 ? Is 40 multiplied by 2 equal to 40 plus 40 ? Is square of negative 3 equal to 9 ? What is the remainder when 340 is divided by 9 ? What is the quotient when 23 is divided by 4 ?

What did we learn ? Data Types Number String Boolean Variables Expression Arithmetic Logical