Unit 2. Srinidhi H Asst Professor

Similar documents
Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

Compound Data Types 2

The second statement selects character number 1 from and assigns it to.

Compound Data Types 1

Lists. There are several ways to create a new list; the simplest is to enclose the elements in square brackets ( [ and ]):

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

CS1 Lecture 11 Feb. 9, 2018

Course Outline - COMP150. Lectures and Labs

Introduction to Python! Lecture 2

COMP1730/COMP6730 Programming for Scientists. Strings

Python Review IPRE

[301] Strings. Tyler Caraza-Harter

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Python Review IPRE

Topic 7: Lists, Dictionaries and Strings

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

Introduction to String Manipulation

Introduction to Python

Introduction to Python

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

UNIVERSITÀ DI PADOVA. < 2014 March >

GE8151- PROBLEM SOLVING AND PYTHON PROGRAMMING Question Bank

Ceng 111 Fall 2015 Week 8a

Programming in Python

Strings. Chapter 6. Python for Everybody

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

How to Design Programs

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Python Programming: Lecture 2 Data Types

Part III Appendices 165

Statements 2. a operator= b a = a operator b

CMSC201 Computer Science I for Majors

MULTIPLE CHOICE. Chapter Seven

OOP and Scripting in Python Advanced Features

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

CS1 Lecture 12 Feb. 11, 2019

The Practice of Computing Using PYTHON. Chapter 4. Working with Strings. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Introduction to Problem Solving and Programming in Python.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

The Practice of Computing Using PYTHON

Computing with Numbers

61A Lecture 2. Wednesday, September 4, 2013

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

"Hello" " This " + "is String " + "concatenation"

Exam 3. 1 True or False. CSCE 110. Introduction to Programming Exam 3

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Python Tutorial. Day 1

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

Get It Interpreter Scripts Arrays. Basic Python. K. Cooper 1. 1 Department of Mathematics. Washington State University. Basics

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

Script language: Python Data structures

Python Class-Lesson1 Instructor: Yao

UNIT-III. All expressions involving relational and logical operators will evaluate to either true or false

Python Intro GIS Week 1. Jake K. Carr

CMSC201 Computer Science I for Majors

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

If Statements, For Loops, Functions

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Real Python: Python 3 Cheat Sheet

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

MITOCW watch?v=rvrkt-jxvko

Introduction to Python

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CIS192 Python Programming

CSC326 Python Sequences i. CSC326 Python Sequences

Python Basics 본자료는다음의웹사이트를정리한내용이니참조바랍니다. PythonBasics

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

Python for loops. Girls Programming Network School of Information Technologies University of Sydney. Mini-lecture 7

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

CMSC201 Computer Science I for Majors

VALLIAMMAI ENGINEERING COLLEGE

Ceng 111 Fall 2015 Week 7a

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Working with Sequences: Section 8.1 and 8.2. Bonita Sharif

Here n is a variable name. The value of that variable is 176.

Python Lists. What is not a Collection. A List is a kind of Collection. friends = [ 'Joseph', 'Glenn', 'Sally' ]

CPD for GCSE Computing: Practical Sheet 6 February 14

Introduction to: Computers & Programming: Strings and Other Sequences

Python Crash Course. João Pedro Pedroso. Departmento de Ciência de Computadores Faculdade de Ciências, Universidade do Porto

PYTHON CONTENT NOTE: Almost every task is explained with an example

Announcements COMP 141. Accessing Characters Review. Using len function 10/30/2017. Strings II. Reminders: Program 6 - due 11/2

Program Planning, Data Comparisons, Strings

University of Texas at Arlington, TX, USA

3. Conditional Execution

61A Lecture 2. Friday, August 28, 2015

CMSC201 Computer Science I for Majors

Python List built-in methods, accessing elements, selection, slicing, recursive functions, list comprehension

3. Conditional Execution

List Processing Patterns and List Comprehension

Transcription:

Unit 2 Srinidhi H Asst Professor 1

Iterations 2

Python for Loop Statements for iterating_var in sequence: statements(s) 3

Python for While Statements while «expression»: «block» 4

The Collatz 3n + 1 sequence 5

The Collatz 3n + 1 sequence 6

LISTS 7

LISTS A list is an ordered collection of values. The values that make up a list are called its elements, or its items. Lists and strings and other collections that maintain the order of their items are called sequences. 8

To create a list The simplest is to enclose the elements in square brackets [ and ]: The elements of a list don t have to be the same type. 9

Memory model of a list 10

Accessing elements The syntax for accessing the elements of a list the index operator: [] Remember that the indices start at 0: 11

List length The function len returns the length of a list, which is equal to the number of its elements. If you are going to use an integer index to access the list, it is a good idea to use this value as the upper bound of a loop instead of a constant. 12

List Concatenation 13

List slices a[start:end] # items start through end-1 a[start:] # items start through the rest of the array a[:end] # items from the beginning through end-1 a[:] # a copy of the whole array a[start:end:step] # start through not past end, by step 14

List slices 15

List deletion 16

Objects and references There are two possible ways the Python interpreter could arrange its memory: 17

Objects and references- Try this?? 18

Aliasing If we assign one variable to another, both variables refer to the same object. Because the same list has two different names, a and b, we say that it is aliased. Changes made with one alias affect the other: 19

Lists are mutable Lists are mutable, which means we can change their elements. Using the index operator on the left side of an assignment, we can update one of the elements: >>> fruit = ["banana", "apple", "quince"] >>> fruit[0] = "pear" >>> fruit[2] = "orange" >>> fruit [ pear, apple, orange ] 20

Aliasing in Lists and Strings For mutable objects such as Lists aliasing might result in inconsistent values. For immutable objects such as Strings aliasing is helpful in copying value of one variable to another. 21

Cloning lists If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called cloning. The easiest way to clone a list is to use the slice operator: 22

Cloning lists Now we are free to make changes to b without worrying that we ll inadvertently be changing a: 23

List parameters Passing a list as an argument actually passes a reference to the list, not a copy or clone of the list. So parameter passing creates an alias: caller has one variable referencing the list. the called function has an alias. but there is only one underlying list object. 24

List parameters- Program Write a function to initialize a list as things=[1,2,3,4] and return the list as things=[2,4,6,8] 25

List methods 26

List methods- append 27

List methods- example 28

Pure functions and modifiers Functions which take lists as arguments and change them during execution are called modifiers and the changes they make are called side effects. A pure function does not produce side effects. It communicates with the calling program only through parameters, which it does not modify, and a return value. 29

Pure functions example Pure function Main function 30

Nested lists A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list: >>> nested = ["hello", 2.0, 5, [10, 20]] >>> print(nested[3]) [10, 20] To extract an element from the nested list, we can proceed as: 31 OR

Matrices Nested lists are often used to represent matrices. For example, the matrix might be represented as: >>> mx = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 32

Strings 33

Strings >>> ss = "Hello, World!" >>> tt = ss.upper() >>> tt HELLO, WORLD! 34

Working with the parts of a string The indexing operator (Python uses square brackets to enclose the index) selects a single character substring from a string: >>> fruit = "banana" >>> m = fruit[1] >>> print(m) 35

Traversal ix = 0 while ix < len(fruit): letter = fruit[ix] print(letter) ix += 1 prefixes = "JKLMNOPQ" suffix = "ack" for p in prefixes: print(p + suffix) The output of this program is: Jack Kack Lack Mack Nack Oack Pack Qack 36

Slices A substring of a string is obtained by taking a slice. >>> s = "Pirates of the Caribbean" >>> print(s[0:7]) Pirates >>> print(s[11:14]) the >>> print(s[15:24]) Caribbean 37

Program Check whether a given string is palindrome or not using Slicing. 38

Strings are immutable Strings are immutable, which means you can t change an existing string. 39

The in and not in operators The in operator tests for membership. When both of the arguments to in are strings, in checks whether the left argument is a substring of the right argument. 40

Program Write a program using functions that takes a string and removes all the vowels from it. 41

Program 42

The split method It splits a single multi-word string into a list of individual words, removing all the whitespace between them. 43