1 Python Chapter 3 Reading strings and printing. © Samuel Marateck.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Introduction to C Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
1 Functions Samuel Marateck © A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Recitation 1 Programming for Engineers in Python.
Strings. Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes.
An Introduction to Textual Programming
Introduction to Python
General Programming Introduction to Computing Science and Programming I.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Python for Informatics: Exploring Information
Computer Science 101 Introduction to Programming.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
Python Overview  Last week Python 3000 was released  Python 3000 == Python 3.0 == Py3k  Designed to break backwards compatibility with the 2.x.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
CS105 STRING LIST TUPLE DICTIONARY. Characteristics of Sequence What is sequence data type? It stores several objects Each object has an order Each object.
File I/O CMSC 201. Overview Today we’ll be going over: String methods File I/O.
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.
Notes on Python Regular Expressions and parser generators (by D. Parson) These are the Python supplements to the author’s slides for Chapter 1 and Section.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Python - 2 Jim Eng Overview Lists Dictionaries Try... except Methods and Functions Classes and Objects Midterm Review.
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python Objects Charles Severance Python for Everybody
Introduction to Computing Science and Programming I
Introduction to Programming
String Methods Programming Guides.
Introduction to Programming
Strings Chapter 6 Python for Everybody
Notes on Python Regular Expressions and parser generators (by D
String Processing Upsorn Praphamontripong CS 1110
CMPT 120 Topic: Python strings.
Strings Part 1 Taken from notes by Dr. Neil Moore
Chapter 8 - Characters and Strings
Engineering Innovation Center
Strings Chapter 6 Slightly modified by Recep Kaya Göktaş in April Python for Informatics: Exploring Information
Topics Introduction to File Input and Output
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Python - Strings.
Chapter 8 More on Strings and Special Methods
CS 100: Roadmap to Computing
Python for Informatics: Exploring Information
Introduction to Programming
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
590 Scraping – NER shape features
Topics Introduction to File Input and Output
Introduction to Computer Science
IST256 : Applications Programming for Information Systems
The Selection Structure
Chapter 2 Primitive Data Types and Operations
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Introduction to Programming
Topics Introduction to File Input and Output
DATA TYPES AND OPERATIONS
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CMPT 120 Topic: Python strings.
STRING MANUPILATION.
Python Objects Charles Severance Python for Everybody
Python Objects Charles Severance Python for Everybody
Presentation transcript:

1 Python Chapter 3 Reading strings and printing. © Samuel Marateck

2 To write and run a program: 1.On the shell’s file menu click New Window 2. Type your program. 3. Use a # to begin a comment. 4. Use the Edit window to edit your program. 5. Save your program to the proper directory To save, choose save on the file menu. 6. To run your program, click run module on the run menu,

3 When you save your program, type the program’s name a period and then py. For Instance prog1.py. The program you write is called the source program. When you run your program, the compiler produces what is called the object program. If you look at the directory, in our case you will see prog1.pyc; you will not be able to read this.

4 The entity that runs the object program is called the Python virtual machine.

5 The input() statement Input to a program is read using the input() statement and is read as a string, for instance, name = input(‘Type your input’) The variable name is a string variable. The string ‘Type your input’ is printed in the shell when the program is run.

6 Our program is: #our first program name = input(‘Type your input ’) Print(‘Our input is ‘, name)

7 When your run the program, the following appears in the shell: >>> Type your input You then type anything after the word input. Type your input NYU rocks Here we typed NYU rocks.

8 The computer responds: Our input is NYU rocks The entire session is shown on the next slide.

9 >>> Type your input NYU rocks our input is NYU rocks >>>

10 To be able to type the input on another line, place \n at the end of the string: name = input(‘Type your input \n’)

11 After you save the program and run it, and type asd as input, the following appears on the shell: >>> Type your input asd our input is asd >>>

12 The characters \n does not appear in the shell.; \n is called an escape sequence and n is called a control character and forces a a skip to the next line (carriage return).

13 To see the functions that can be used with a string, type dir(‘’) in the shell. The result is:

14 >>> dir('') ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>>

15 To see the effect of one of these functions, type a string, then a period, then the function ending with (). So >>>’asd’.upper() ‘ASD’ upper() converts every letter in the string to uppercase. It leaves all other characters alone. The following are examples of some of these functions.

16 lower()Changes uppercase to Lowercase and leaves all other variables alone. Example: >>>’THE’ ‘the’

17 isalpha()Returns True if the string consists of only letters else False. Example: >>> ‘the’.isalpha() TRUE >>>’the12’. isalpha() False

18 capitalize(). Capitalizes a string. Example: >>>’asd’. capitalize() ‘Asd’

19 strip() Removes the leading and trailing blanks. Example: >>>‘ asd ‘.strip() ‘asd’

20 To convert numeric input to an integer, use the int() function. value = input(‘type your integer’) number = int(value) print(number + 3)

21 Normally, two print statements print their output on two separate lines, Thus print(‘abc’) print(‘def’) Produces >>> abc def >>>

22 To have the output on one line, end the first print with end= ‘’ print(’abc’, end = ‘’) print(‘def’) produces >>>abcdef

23 We’ll investigate the following program: length = 87 width = 5 area = length*width print(‘area =‘, area)

24 If we wrote prin(‘def’) the computer would respond with Traceback (most recent call last): File " ", line 1, in prin('def') NameError: name 'prin' is not defined

25 This is another example of a syntax error. It is also called a compilation error since it occurs during compilation time. Such an error must be corrected before the program will run.

26 The following table describes how the variables are defined. undef means undefined.

27 statementlengthwidtharea length = 8787undef width = 5875undef area=length*width print875435

28 Now let’s analyze: length = 87 width = 5 area = length*width print(‘area =‘, area) width = 3 print(‘area =‘, area)

29 We see that the width has been redefined. What happens in the computer’s memory is that the bits in the location width are reconfigured.

30 statementlengthwidtharea length = 8787undef width = 5875undef area=length*width print width = print873435

31 We see that since the area does not appear on the left side of an assignment statement, it is not redefined and the original value of the area will be printed. This is called a logical error because it is an error in logic and will produce the wrong results. You must detect a logical error yourself.

32 In the corrected program we recalculate the area as is shown in the next slide.

33 length = 87 width = 5 area = length*width print(‘area =‘, area) width = 3 area = length*width print(‘area =‘, area)