Imperative Programming

Slides:



Advertisements
Similar presentations
Introduction to Computing Using Python Imperative Programming  Python Programs  Interactive Input/Output  One-Way and Two-Way if Statements  for Loops.
Advertisements

Μαθαίνοντας Python [Κ4] ‘Guess the Number’
16-May-15 Sudden Python Drinking from the Fire Hose.
Conditional Statements Introduction to Computing Science and Programming I.
 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 Python and programming Michael Ernst UW CSE 190p Summer 2012.
Python Programming Fundamentals
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
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.
Introduction to Computing Using Python Imperative Programming  what is a Python program?  print() statement  input() statement  type conversion statements.
Variables and Expressions CMSC 201 Chang (rev )
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Python Conditionals chapter 5
Decision Structures, String Comparison, Nested Structures
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Input, Output and Variables GCSE Computer Science – Python.
Fundamentals of Programming I Overview of Programming
Basic concepts of C++ Presented by Prof. Satyajit De
Agenda Introduction Computer Programs Python Variables Assignment
Input and Output Upsorn Praphamontripong CS 1110
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
COMPSCI 107 Computer Science Fundamentals
Chapter 2 - Introduction to C Programming
Topics Introduction to Repetition Structures
Python: Control Structures
Chapter 3 Instructor: Zhuojun Duan
Introduction to Programming
Chapter 2 - Introduction to C Programming
PH2150 Scientific Computing Skills
Imperative Programming
Decision Structures, String Comparison, Nested Structures
Line Continuation, Output Formatting, and Decision Structures
Introduction to Programming
Chapter 2 - Introduction to C Programming
Topics Introduction to File Input and Output
And now for something completely different . . .
Decision Structures, String Comparison, Nested Structures
Chapter 2 - Introduction to C Programming
Introduction to Primitive Data types
Introduction to Programming
More Looping Structures
3. Decision Structures Rocky K. C. Chang 19 September 2018
Chapter 2 - Introduction to C Programming
Introduction to Python
15-110: Principles of Computing
Python Basics with Jupyter Notebook
Nate Brunelle Today: Conditional Decision Statements
Chapter 2 - Introduction to C Programming
Terminal-Based Programs
COMPUTER PROGRAMMING SKILLS
Introduction to Programming
More Looping Structures
Topics Introduction to File Input and Output
Introduction to C Programming
def-ining a function A function as an execution control structure
Hardware is… Software is…
CMPT 120 Lecture 6 – Unit 1 – Chatbots
Introduction to Primitive Data types
Data Types and Expressions
Introduction to Python
Presentation transcript:

Imperative Programming Introduction to Computing Using Python Imperative Programming What is a Python program? print() statement input() statement Type conversion statements str(), int(), float() if/elif/else conditional statement

Python program A Python program is a sequence of Python statements Introduction to Computing Using Python line1 = 'Hello Python developer...' A Python program is a sequence of Python statements Stored in a text file with the extension .py Executed using an integrated development environment (IDE) or “from the command line” line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) $ python hello.py Hello Python developer… $ python hello.py Hello Python developer… Welcome to the world of Python! $ python hello.py hello.py

Built-in function print() Introduction to Computing Using Python The builtin function print() echoes its parameter to the output window The argument can be any object: an integer, a float, a string, a list … The “string representation” of the object is printed When you output a string using print, the quote marks around the string are omitted Be default, a print statement ends its output with a newline (return). But you can change this to any string you like, including the empty string. Try print('hello', end='!!!') >>> print(0) >>> print(0.0) 0.0 >>> print('zero') zero >>> print([0, 1, 'two']) [0, 1, 'two']

Built-in function input() Introduction to Computing Using Python The builtin function input() requests and reads input from the user interactively Its (optional) input argument is the request message Typically used on the right side of an assignment statement When executed: input prints the request message input accepts typed input from the user The user input may then be assigned to the variable on the left side of the assignment statement >>> first = input('Your first name: ') Your first name: Michael >>> last = input('Your last name: ') Your last name: Rojas >>> first + ' ' + last 'Michael Rojas'

Change string input to another type Introduction to Computing Using Python Function input() treats anything the user enters as a string >>> age = input('Enter your age: ') Enter your age: 18 >>> age '18' >>> int(age) 18 What if we want the user to interactively enter a number? Use a type conversion function int() changes a string, Boolean or float type to an int float() changes a string, Boolean or int type to a float str() changes an int, float or Boolean type to a string

Exercise Write a program that: Requests the user’s name Introduction to Computing Using Python Write a program that: Requests the user’s name Requests the user’s age Computes the user’s age one year from now and prints the message shown >>> Enter your name: Marie Enter your age: 17 Marie, you will be 18 next year! name = input('Enter your name: ') age = int(input('Enter your age: ')) line = name + ', you will be ' + str(age+1) + ' next year!' print(line)

Exercise Write a program that: Introduction to Computing Using Python Write a program that: Requests the user’s name Requests the user’s age Prints a message saying whether the user is eligible to vote or not To do this, we need a way to execute a Python statement if a condition is true

print('Be sure to drink liquids.') if statement Introduction to Computing Using Python if <condition>: <indented code block> <non-indented statement> if temp > 86: print('It is hot!') print('Be sure to drink liquids.') print('Goodbye.') if temp > 86: print('It is hot!') print('Be sure to drink liquids.') print('Goodbye.') if temp > 86: print('It is hot!') print('Be sure to drink liquids.') print('Goodbye.') The value of temp is 50. The value of temp is 90. temp > 86: True print('It is hot!') False print('Be sure to drink liquids.') print('Goodbye.')

Exercises Write corresponding if statements: Introduction to Computing Using Python Write corresponding if statements: If age is greater than 62, print 'You can get Social Security benefits’ If 'large bonuses' appears in the string report print 'Vacation time!' If hits is greater than 10 and shield equals 0, print "You're dead..." >>> hits = 12 >>> shield = 0 >>> if hits > 10 and shield == 0: print("You're dead...") You're dead... >>> hits, shield = 12, 2 >>>

Indentation is critical Introduction to Computing Using Python if temp > 86: print('It is hot!') print('Drink liquids.') print('Goodbye.') if temp > 86: print('It is hot!') print('Drink liquids.') print('Goodbye.') temp > 86: temp > 86: True True print('It is hot!') print('It is hot!') False False print('Drink liquids.') print('Drink liquids.') print('Goodbye.') print('Goodbye.')

if/elif/else statement Introduction to Computing Using Python if <condition>: <indented code block 1> elif: <indented code block 2> else: <indented code block 3> <non-indented statement> if age > 70: print('Elvis!') elif age > 60: print('Beatles!') else: print('Disco!') print('Goodbye') The value of age is 20. age > 70: age > 60: False False print('Disco!') True True print('Elvis!') print('Beatles!') print('Goodbye')

Exercise Introduction to Computing Using Python Extend this music preference program by adding more elif clauses so that: It requests the user’s name It requests the user’s age It prints a message with the user’s musical preference for all ages, by decade >>> Enter your name: Marie Enter your age: 82 Marie, you like Elvis. >>> ============RESTART================ Enter your age: 40 Marie, you like Sinead. name = input('Enter your name: ') age = int(input('Enter your age: ')) if age > 70: print(name + ", you like Elvis.") else: print(name + ", you like Sinead.")