SlideShare a Scribd company logo
1 of 23
Download to read offline
Introduction to Python

          Lecture 3
       Kasyanov Anton



          IASA 2011
Plan

   Strings
   Modules
   Methods
   While loop
Strings

   Sequences of characters.
   Two types str and unicode.
       We'll use str in this course.
       It contains the roman alphabet, numbers a few
        symbols.
       Unicode is larger, contains more accented letters,
        Chinese characters, and more.
   Strings are denoted by single or double quotes.
       Quote type must match!
Strings

   Strings can be 'added'.
        We call this concatenation.
        “str” + “ing” results in “string”.
   Can also be multiplied, sort of.
        You can't multiply a string with itself, but the
         multiplication operator functions as a copy.
        So “copy”*3 results in “copycopycopy”.
   Can also compare strings using relational operators.
   Can check if substrings are in a string using in.
   Long strings that span multiple lines can be made using '''.
Escape characters

   Denoted by a backslash, they indicate to
    python that the next character is a special
    character.
       n - a new line
       ' - a single quote
       ” - a double quote
        - a backslash
       t - a tab.
   Aside len(string) will return an int that is
    the number of characters in the string.
Converting

   If we have a variable that is not a string and we
    want to add it to a string, we need to convert it.
   We use str(x) to convert x to a string.
   Print will display the variable, and can display
    mixed types.
       They must be separated with a comma.
       print “string”, x, “ “, real_num
Formatting

   Can use special characters to tell python to
    insert a type into a string.
   print “My age is %d.” % age
   The %d tells python to take age, and format it
    as an integer.
   %s says to take a value and format it as a
    string.
   %f says to take a value and format it as a float.
   %.2f says to pad the float to 2 decimal places.
Formatting with multiple
                variables
   What if we want multiple variables in our string?
       print “Person“, name, “has height”, 
          height, “age“, age, “weight“, weight
   We put them in parentheses separated by
    commas.
       print “Person %s has weight %.2f 
           and age %d and height %d.“ 
           % (name, weight, age, height)
User input

   Just one function
   raw_input(string) – shows a prompt to user and
    returns inputted string
   It doesn't return int or float so don't forget to
    convert types.
Modules

   Sometimes we want to use other people's
    code.
   Or make our own code available for use.
   It's convenient if we can bundle up related
    functions in one file.
   Modules allow us to do this.
   A Module is a group of related functions and
    variables.
Using modules

   To use a module, you need to import it.
   Importing a module causes python to run each
    line of code in the module.
       It it is just function definitions this doesn't cause
        much trouble.
       But it can be annoying if there is code that you don't
        care about in the module.
   To use a function in a module use
        module_name.function_name()
__name__

   In addition to variables that are defined in the
    module, each module has a variable that is
    called __name__.
   If we import a module called module_m, then
        module_m.__name__ == “module_m”
   But if we run a module, then
       __name__ == “__main__”
   Recall that if we are running a module, we don't
    need the module name as a prefix.
Other way to import things

   from module_name import fn_name1, fn_name2
       Will import fn_name1 and fn_name 2
       Can be referred to by just fn_name1()
   Can also use * as a wildcard to import all the
    functions.
       from module_name import *
   What if two modules have a function with the
    same name?
   The most recent stays.
Values methods

   We've seen that modules can have their own
    functions.
   A similar thing is true for values.
   Values contain ways that you can modify them.
    We call these methods.
   These are called by value.fn_name()
   Or, if we've assigned a value to a variable we
    can use variable_name.fn_name()
   We can call help(type) to figure out what
    methods a type has available to it.
String methods

   Can find them by using help(str).
   Useful ones include:
   s.replace(old, new) - return a new string
    that is a copy of s with all instances of old
    replaced by new.
   string.count(substr) – return the number
    of instances of substr in the string.
   string.lower() - shift to lower case letters.
   string.upper() - shift to capitalised letters.
Remember!

   Functions belong to modules.
   Methods belong to objects.
       All of the basic types in python are objects.
       We will learn how to make our own later.
       This is covered in greater detail in 148.
   len(str) is a function
   str.lower() is a method.
   Subtle but important distinction.
While loop

   Instead Python uses loops.
       We will cover the for loop next week.
   The while loop has the form:
        while condition:
             block
   The condition is checked first. If it evaluates to
    True, the block executes, otherwise the block is
    skipped, and the next line of code is executed.
Home assignment

   You need to create a module "greetings.py"
    witch contains following functions:
   1)greet_birthday(name, author, age, wish) -
    should return string that contains birthday
    greeting
   2)greet_new_year(name, author, year, wish) -
    should return string that contains new year
    greeting
   3)make_invitation(name, author, date, time,
    place, purpose) - ...
Home assignment

   If you run this module it have to ask for the kind
    of greeting and then ask parameters of it.
   Also create a module "greet_john.py" that
    makes a birthday greeting for John who is 23
    years old now by Mike who wish John long life
    and pure love.
Home assignment

   Dear John!
   I would like to greet you with your birthday! For
    23 years old man you're very mature and wise
    person.
   I wish you long life and pure love.

   Yours, Mike.
Home assignment

   Send files to mind_master@ukr.net due to
    Monday
Introduction to Python Lecture 3 Strings, Modules, Methods and While Loops
Introduction to Python Lecture 3 Strings, Modules, Methods and While Loops

More Related Content

What's hot

String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi Kant Sahu
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and StringsEduardo Bergavera
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi Kant Sahu
 
Handout # 6 pointers and recursion in c++
Handout # 6   pointers and recursion in c++Handout # 6   pointers and recursion in c++
Handout # 6 pointers and recursion in c++NUST Stuff
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Edureka!
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)Akshay soni
 
Computer science solution - programming - big c plus plus
Computer science   solution - programming - big c plus plusComputer science   solution - programming - big c plus plus
Computer science solution - programming - big c plus plusPraveen Tyagi
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov
 

What's hot (19)

String and string buffer
String and string bufferString and string buffer
String and string buffer
 
JVM and OOPS Introduction
JVM and OOPS IntroductionJVM and OOPS Introduction
JVM and OOPS Introduction
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Strings
StringsStrings
Strings
 
Python strings
Python stringsPython strings
Python strings
 
package
packagepackage
package
 
String Handling
String HandlingString Handling
String Handling
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Java
JavaJava
Java
 
Handout # 6 pointers and recursion in c++
Handout # 6   pointers and recursion in c++Handout # 6   pointers and recursion in c++
Handout # 6 pointers and recursion in c++
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
Computer science solution - programming - big c plus plus
Computer science   solution - programming - big c plus plusComputer science   solution - programming - big c plus plus
Computer science solution - programming - big c plus plus
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 

Similar to Introduction to Python Lecture 3 Strings, Modules, Methods and While Loops

ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxpriestmanmable
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfMegMeg17
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxfaithxdunce63732
 
Python Strings Format
Python Strings FormatPython Strings Format
Python Strings FormatMr Examples
 
Python Traning presentation
Python Traning presentationPython Traning presentation
Python Traning presentationNimrita Koul
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3Syed Farjad Zia Zaidi
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And AnswersH2Kinfosys
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumarSujith Kumar
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfrajkumar2792005
 

Similar to Introduction to Python Lecture 3 Strings, Modules, Methods and While Loops (20)

ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdf
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Python Strings Format
Python Strings FormatPython Strings Format
Python Strings Format
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
python (1).pdf
python (1).pdfpython (1).pdf
python (1).pdf
 
LectureNotes-04-DSA
LectureNotes-04-DSALectureNotes-04-DSA
LectureNotes-04-DSA
 
Python Traning presentation
Python Traning presentationPython Traning presentation
Python Traning presentation
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
unit 1.docx
unit 1.docxunit 1.docx
unit 1.docx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
 

More from Anton Kasyanov

spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21Anton Kasyanov
 
Introduction to Computer Vision (uapycon 2017)
Introduction to Computer Vision (uapycon 2017)Introduction to Computer Vision (uapycon 2017)
Introduction to Computer Vision (uapycon 2017)Anton Kasyanov
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov
 
Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov, Introduction to Python, Lecture1Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov, Introduction to Python, Lecture1Anton Kasyanov
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov
 

More from Anton Kasyanov (6)

spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21
 
Introduction to Computer Vision (uapycon 2017)
Introduction to Computer Vision (uapycon 2017)Introduction to Computer Vision (uapycon 2017)
Introduction to Computer Vision (uapycon 2017)
 
aiohttp intro
aiohttp introaiohttp intro
aiohttp intro
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
 
Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov, Introduction to Python, Lecture1Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov, Introduction to Python, Lecture1
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
 

Introduction to Python Lecture 3 Strings, Modules, Methods and While Loops

  • 1. Introduction to Python Lecture 3 Kasyanov Anton IASA 2011
  • 2. Plan  Strings  Modules  Methods  While loop
  • 3. Strings  Sequences of characters.  Two types str and unicode.  We'll use str in this course.  It contains the roman alphabet, numbers a few symbols.  Unicode is larger, contains more accented letters, Chinese characters, and more.  Strings are denoted by single or double quotes.  Quote type must match!
  • 4. Strings  Strings can be 'added'.  We call this concatenation.  “str” + “ing” results in “string”.  Can also be multiplied, sort of.  You can't multiply a string with itself, but the multiplication operator functions as a copy.  So “copy”*3 results in “copycopycopy”.  Can also compare strings using relational operators.  Can check if substrings are in a string using in.  Long strings that span multiple lines can be made using '''.
  • 5. Escape characters  Denoted by a backslash, they indicate to python that the next character is a special character.  n - a new line  ' - a single quote  ” - a double quote  - a backslash  t - a tab.  Aside len(string) will return an int that is the number of characters in the string.
  • 6. Converting  If we have a variable that is not a string and we want to add it to a string, we need to convert it.  We use str(x) to convert x to a string.  Print will display the variable, and can display mixed types.  They must be separated with a comma.  print “string”, x, “ “, real_num
  • 7. Formatting  Can use special characters to tell python to insert a type into a string.  print “My age is %d.” % age  The %d tells python to take age, and format it as an integer.  %s says to take a value and format it as a string.  %f says to take a value and format it as a float.  %.2f says to pad the float to 2 decimal places.
  • 8. Formatting with multiple variables  What if we want multiple variables in our string?  print “Person“, name, “has height”, height, “age“, age, “weight“, weight  We put them in parentheses separated by commas.  print “Person %s has weight %.2f and age %d and height %d.“ % (name, weight, age, height)
  • 9. User input  Just one function  raw_input(string) – shows a prompt to user and returns inputted string  It doesn't return int or float so don't forget to convert types.
  • 10. Modules  Sometimes we want to use other people's code.  Or make our own code available for use.  It's convenient if we can bundle up related functions in one file.  Modules allow us to do this.  A Module is a group of related functions and variables.
  • 11. Using modules  To use a module, you need to import it.  Importing a module causes python to run each line of code in the module.  It it is just function definitions this doesn't cause much trouble.  But it can be annoying if there is code that you don't care about in the module.  To use a function in a module use module_name.function_name()
  • 12. __name__  In addition to variables that are defined in the module, each module has a variable that is called __name__.  If we import a module called module_m, then module_m.__name__ == “module_m”  But if we run a module, then  __name__ == “__main__”  Recall that if we are running a module, we don't need the module name as a prefix.
  • 13. Other way to import things  from module_name import fn_name1, fn_name2  Will import fn_name1 and fn_name 2  Can be referred to by just fn_name1()  Can also use * as a wildcard to import all the functions.  from module_name import *  What if two modules have a function with the same name?  The most recent stays.
  • 14. Values methods  We've seen that modules can have their own functions.  A similar thing is true for values.  Values contain ways that you can modify them. We call these methods.  These are called by value.fn_name()  Or, if we've assigned a value to a variable we can use variable_name.fn_name()  We can call help(type) to figure out what methods a type has available to it.
  • 15. String methods  Can find them by using help(str).  Useful ones include:  s.replace(old, new) - return a new string that is a copy of s with all instances of old replaced by new.  string.count(substr) – return the number of instances of substr in the string.  string.lower() - shift to lower case letters.  string.upper() - shift to capitalised letters.
  • 16. Remember!  Functions belong to modules.  Methods belong to objects.  All of the basic types in python are objects.  We will learn how to make our own later.  This is covered in greater detail in 148.  len(str) is a function  str.lower() is a method.  Subtle but important distinction.
  • 17. While loop  Instead Python uses loops.  We will cover the for loop next week.  The while loop has the form: while condition: block  The condition is checked first. If it evaluates to True, the block executes, otherwise the block is skipped, and the next line of code is executed.
  • 18. Home assignment  You need to create a module "greetings.py" witch contains following functions:  1)greet_birthday(name, author, age, wish) - should return string that contains birthday greeting  2)greet_new_year(name, author, year, wish) - should return string that contains new year greeting  3)make_invitation(name, author, date, time, place, purpose) - ...
  • 19. Home assignment  If you run this module it have to ask for the kind of greeting and then ask parameters of it.  Also create a module "greet_john.py" that makes a birthday greeting for John who is 23 years old now by Mike who wish John long life and pure love.
  • 20. Home assignment  Dear John!  I would like to greet you with your birthday! For 23 years old man you're very mature and wise person.  I wish you long life and pure love.  Yours, Mike.
  • 21. Home assignment  Send files to mind_master@ukr.net due to Monday