SlideShare a Scribd company logo
6.189: Introduction to Programming in Python
Session 1

Course Syllabus
1. Administrivia. Variables and Types.
2. Functions. Basic Recursion.
3. Control Flow: Branching and Repetition.
4. Introduction to Objects: Strings and Lists.
5. Project 1: Structuring Larger Programs.

6. Python Modules. Debugging Programs.
7. Introduction to Data Structures: Dictionaries.
8. Functions as a Type, Anonymous Functions and List Comprehensions
9. Project 2: Working in a Team.
10. Quiz. Wrap-up.

Administrivia
1. Grading. Well, it’s a pass/fail course – attend the classes, do the homework. Attendance is important:
email us in advance if you have to miss a class.

2. Optional Assignment(s). There’s an optional assignment that you can work on when you have free
time, e.g. if you finish the class lab period early. This assignment is completely optional – it was designed
to give you a better intuition for programming and to help in later course 6 studies.


3 . Textbook. This class uses readings from the online textbook How to Think Like a Computer Scientist –
its always nice to have two perspectives on concepts. You can find this textbook at the following link:

              http://www.openbookproject.net/thinkcs/python/english2e/index.xhtml

Notes/Homework
1. Install Python. You can get the latest version at www.python.org.

     ­ Linux and OS X users: You should already have Python installed – to test this, run the command
       python in console mode. Note that the first line tells you the version number: you may want to
       upgrade your installation if you have a version earlier than 2.3.

     ­ Windows users: Grab the windows installer from the Downloads section. After installation, you
       can run the IDLE (Python GUI) command from the Start menu.
2. Reading. Read 1.1-1.2, 1.8-1.9, and chapter 2 from the textbook.

3. Writing Programs. Recall that a program is just a set of instructions for the computer to execute. Let’s
start with a basic command:

print x: Prints the value of the expression x, followed by a newline.

    Program Text:

      print “Hello World!”

      print “How are you?”



    Output:

      Hello World!

      How are you?



(The compiler needs those quotation marks to identify strings – we’ll talk about this later.) Don’t worry,
we’ll be adding many more interesting commands later! For now, though, we’ll use this to have you
become familiar with the administrative details of writing a program.

Write a program that, when run, prints out a tic-tac-toe board.

     ­ Linux and OS X users: To write your program, just create a text file with the contents of the
       program text (e.g. the program text above.) To run your program (pretend its named
       first_print.py), use the command python first_print.py at the shell.

     ­ Windows users: The best way to program in Windows is using the IDLE GUI (from the shortcut
       above.) To create a new program, run the command File->New Window (CTRL+N) – this will open
       up a blank editing window. In general, save your program regularly; after saving, you can hit F5 to
       run your program and see the output.

Make sure to write your program using the computer at least once! The purpose of this question is to
make sure you know how to write programs using your computing environment; many students in
introductory courses experience trouble with assignments not because they have trouble with the
material but because of some weird environment quirk.

You can write the answer to the question in the box below (I know its hard to show spaces while writing
– just do your best)

    Program Text:
Expected Output:

       | |

      -----
       | |

      -----
       | |


4. Interpreter Mode. Python has a write-as-you-go mode that’s useful when testing small snippets of
code. You can access this by running the command python at the shell (for OS X and Linux) or by starting
the IDLE GUI (for Windows) – you should see a >>> prompt. Try typing a print command and watch what
happens.

You’ll find that its much more convenient to solve a lot of the homework problems using the interpreter.
When you want to write an actual, self-contained program (e.g. writing the game tic-tac-toe), follow the
instructions in section 3 above.

5. Variables. Put simply, variables are containers for storing information. For example:

    Program Text:

      a = “Hello World!”
      print a


    Output:

      Hello World!



The = sign is an assignment operator which says: assign the value “Hello World!” to the variable a.

    Program Text:

      a = “Hello World!”
      a = “and goodbye..”
      print a


    Output:

      and goodbye..



Taking this second example, the value of a after executing the first line above is “Hello World!”, and
after the second line its “and goodbye..” (which is what gets printed)
In Python, variables are designed to hold specific types of information. For example, after the first
command above is executed, the variable a is associated with the string type. There are several types of
information that can be stored:

     ­   Boolean. Variables of this type can be either True or False.
     ­   Integer. An integer is a number without a fractional part, e.g. -4, 5, 0, -3.
     ­   Float. Any rational number, e.g. 3.432. We won’t worry about floats for today.
     ­   String. Any sequence of characters. We’ll get more in-depth with strings later in the week.

Python (and many other languages) are zealous about type information. The string “5” and integer 5 are
completely different entities to Python, despite their similar appearance. You’ll see the importance of
this in the next section.

Write a program that stores the value 5 in a variable a and prints out the value of a, then stores the
value 7 in a and prints out the value of a (4 lines.)

    Program Text:




    Expected Output:

      5

      7



6. Operators. Python has the ability to be used as a cheap, 5-dollar calculator. In particular, it supports
basic mathematical operators: +, -, *, /.

    Program Text:

      a = 5 + 7
      print a


    Output:

      12



Variables can be used too.

    Program Text:

      a = 5
      b = a + 7
      print b
Output:

      12



Expressions can get fairly complex.

    Program Text:

      a = (3+4+21) / 7
      b = (9*4) / (2+1) - 6
      print (a*b)-(a+b)

    Output:

      14


These operators work on numbers. Type information is important – the following expressions would
result in an error.

                                  “Hello” / 123       “Hi” + 5      “5” + 7

The last one is especially important! Note that Python just sees that 5 as a string – it has no concept of it
possibly being a number.

Some of the operators that Python includes are

     ­ Addition, Subtraction, Multiplication. a+b, a-b, and a*b respectively.
     ­ Integer Division. a/b. Note that when division is performed with two integers, only the quotient
       is returned (the remainder is ignored.) Try typing print 13/6 into the interpreter
     ­ Exponentiation (ab). a ** b.

Operators are evaluated using the standard order of operations. You can use parentheses to force

certain operators to be evaluated first.


Let’s also introduce one string operation.


     ­ Concatenation. a+b. Combines two strings into one. “Hel” + “lo” would yield “Hello”

Another example of type coming into play! When Python sees a + b, it checks to see what type a and b
are. If they are both strings then it concatenates the two; if they are both integers it adds them; if one is

a string and the other is an integer, it returns an error.


Write the output of the following lines of code (if an error would result, write error):
print 13 + 6                                    Output: _______________________________________
print 2 ** 3                                    Output: _______________________________________
print 2 * (1 + 3)                               Output: _______________________________________
print 8 / 9                                     Output: _______________________________________
print “13” + “6”                                Output: _______________________________________
print “13” + 6                                  Output: _______________________________________

7. Review. Here are the important concepts covered today.

     ­ What is a program? How does one write them.
     ­ Using the interpreter mode.
     ­ Variables: containers for storing information. Variables are typed – each variable only holds
       certain types of data (one might be able to store strings, another might be able to store numbers,
       etc.)
     ­ Operators: +, -, *, /, **, and parentheses.

More Related Content

PDF
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
PPTX
C++ Tutorial
PPTX
C++ lecture 01
PDF
Basic Concepts in Python
PDF
C programming notes
DOC
1. introduction to computer
PPT
Help with Pyhon Programming Homework
PDF
C programming session7
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
C++ Tutorial
C++ lecture 01
Basic Concepts in Python
C programming notes
1. introduction to computer
Help with Pyhon Programming Homework
C programming session7

What's hot (20)

PPTX
AS TASKS #8
DOCX
Let's us c language (sabeel Bugti)
PPTX
C++ Basics introduction to typecasting Webinar Slides 1
PDF
Notes1
PPSX
Complete C++ programming Language Course
PPT
Fundamental of C Programming Language and Basic Input/Output Function
PPTX
Mastering Python lesson 4_functions_parameters_arguments
PDF
Handout#01
PPT
Programming of c++
PDF
C programming language
PPT
First draft programming c++
PDF
Assignment4
PDF
Embedded SW Interview Questions
PPT
Declaration of variables
PPT
C language Unit 2 Slides, UPTU C language
PDF
Basic Information About C language PDF
PPTX
PPT
Chapter3
AS TASKS #8
Let's us c language (sabeel Bugti)
C++ Basics introduction to typecasting Webinar Slides 1
Notes1
Complete C++ programming Language Course
Fundamental of C Programming Language and Basic Input/Output Function
Mastering Python lesson 4_functions_parameters_arguments
Handout#01
Programming of c++
C programming language
First draft programming c++
Assignment4
Embedded SW Interview Questions
Declaration of variables
C language Unit 2 Slides, UPTU C language
Basic Information About C language PDF
Chapter3
Ad

Viewers also liked (17)

PPTX
Presentación2
PDF
Smm project 1
PPTX
Trabajo de lengua siglo xviii
PDF
Qosmet 20160219
PPTX
1000 keek followers free
PPTX
Tusc movie
PDF
Whitney - Comm Law Paper
PPTX
Management Quiz
PDF
Innovation for SME Businesses- Fasttrack for Innovation - Horizon 2020 Funding
DOCX
Punim seminarik
PPTX
10 la palabra y la oracion
PDF
Methods of Documentation RSC Final Report
DOCX
Publisher
PPS
Cary Nadler, Phd.
PPT
Презентация на тему: «Административные барьеры в процессах международной торг...
PPTX
Shamit khemka list outs 6 technology trends for 2015
PDF
3430_3440Brochure_Final_PRINT
Presentación2
Smm project 1
Trabajo de lengua siglo xviii
Qosmet 20160219
1000 keek followers free
Tusc movie
Whitney - Comm Law Paper
Management Quiz
Innovation for SME Businesses- Fasttrack for Innovation - Horizon 2020 Funding
Punim seminarik
10 la palabra y la oracion
Methods of Documentation RSC Final Report
Publisher
Cary Nadler, Phd.
Презентация на тему: «Административные барьеры в процессах международной торг...
Shamit khemka list outs 6 technology trends for 2015
3430_3440Brochure_Final_PRINT
Ad

Similar to pyton Notes1 (20)

PDF
Python for Physical Science.pdf
PPTX
Python fundamentals
PDF
introduction to python programming course
PPTX
python introduction initial lecture unit1.pptx
PDF
Intro-to-Python-Part-1-first-part-edition.pdf
PPTX
Unit 8.4Testing condition _ Developing Games.pptx
PDF
Introduction to python
PPT
Introduction to Procedural Programming in C++
DOCX
PYTHON NOTES
 
PPT
Intro. to prog. c++
PPT
Spsl iv unit final
PPT
Spsl iv unit final
PPTX
Python training
PPTX
pythontraining-201jn026043638.pptx
PDF
Fundamentals of python
PDF
C++ In One Day_Nho Vĩnh Share
PDF
265 ge8151 problem solving and python programming - 2 marks with answers
PPT
Introduction to python
DOCX
The basics of c programming
Python for Physical Science.pdf
Python fundamentals
introduction to python programming course
python introduction initial lecture unit1.pptx
Intro-to-Python-Part-1-first-part-edition.pdf
Unit 8.4Testing condition _ Developing Games.pptx
Introduction to python
Introduction to Procedural Programming in C++
PYTHON NOTES
 
Intro. to prog. c++
Spsl iv unit final
Spsl iv unit final
Python training
pythontraining-201jn026043638.pptx
Fundamentals of python
C++ In One Day_Nho Vĩnh Share
265 ge8151 problem solving and python programming - 2 marks with answers
Introduction to python
The basics of c programming

More from Amba Research (20)

PPT
Case Econ08 Ppt 16
PPT
Case Econ08 Ppt 08
PPT
Case Econ08 Ppt 11
PPT
Case Econ08 Ppt 14
PPT
Case Econ08 Ppt 12
PPT
Case Econ08 Ppt 10
PPT
Case Econ08 Ppt 15
PPT
Case Econ08 Ppt 13
PPT
Case Econ08 Ppt 07
PPT
Case Econ08 Ppt 06
PPT
Case Econ08 Ppt 05
PPT
Case Econ08 Ppt 04
PPT
Case Econ08 Ppt 03
PPT
Case Econ08 Ppt 02
PPT
Case Econ08 Ppt 01
PDF
pyton Notes9
PDF
PDF
PDF
pyton Notes6
PDF
pyton Exam1 soln
Case Econ08 Ppt 16
Case Econ08 Ppt 08
Case Econ08 Ppt 11
Case Econ08 Ppt 14
Case Econ08 Ppt 12
Case Econ08 Ppt 10
Case Econ08 Ppt 15
Case Econ08 Ppt 13
Case Econ08 Ppt 07
Case Econ08 Ppt 06
Case Econ08 Ppt 05
Case Econ08 Ppt 04
Case Econ08 Ppt 03
Case Econ08 Ppt 02
Case Econ08 Ppt 01
pyton Notes9
pyton Notes6
pyton Exam1 soln

Recently uploaded (20)

PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
Machine Learning_overview_presentation.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
1. Introduction to Computer Programming.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Tartificialntelligence_presentation.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Approach and Philosophy of On baking technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Encapsulation_ Review paper, used for researhc scholars
Univ-Connecticut-ChatGPT-Presentaion.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
Machine Learning_overview_presentation.pptx
A Presentation on Artificial Intelligence
A comparative study of natural language inference in Swahili using monolingua...
1. Introduction to Computer Programming.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Tartificialntelligence_presentation.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Assigned Numbers - 2025 - Bluetooth® Document
MIND Revenue Release Quarter 2 2025 Press Release
Diabetes mellitus diagnosis method based random forest with bat algorithm
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Approach and Philosophy of On baking technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Per capita expenditure prediction using model stacking based on satellite ima...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Encapsulation_ Review paper, used for researhc scholars

pyton Notes1

  • 1. 6.189: Introduction to Programming in Python Session 1 Course Syllabus 1. Administrivia. Variables and Types. 2. Functions. Basic Recursion. 3. Control Flow: Branching and Repetition. 4. Introduction to Objects: Strings and Lists. 5. Project 1: Structuring Larger Programs. 6. Python Modules. Debugging Programs. 7. Introduction to Data Structures: Dictionaries. 8. Functions as a Type, Anonymous Functions and List Comprehensions 9. Project 2: Working in a Team. 10. Quiz. Wrap-up. Administrivia 1. Grading. Well, it’s a pass/fail course – attend the classes, do the homework. Attendance is important: email us in advance if you have to miss a class. 2. Optional Assignment(s). There’s an optional assignment that you can work on when you have free time, e.g. if you finish the class lab period early. This assignment is completely optional – it was designed to give you a better intuition for programming and to help in later course 6 studies. 3 . Textbook. This class uses readings from the online textbook How to Think Like a Computer Scientist – its always nice to have two perspectives on concepts. You can find this textbook at the following link: http://www.openbookproject.net/thinkcs/python/english2e/index.xhtml Notes/Homework 1. Install Python. You can get the latest version at www.python.org. ­ Linux and OS X users: You should already have Python installed – to test this, run the command python in console mode. Note that the first line tells you the version number: you may want to upgrade your installation if you have a version earlier than 2.3. ­ Windows users: Grab the windows installer from the Downloads section. After installation, you can run the IDLE (Python GUI) command from the Start menu.
  • 2. 2. Reading. Read 1.1-1.2, 1.8-1.9, and chapter 2 from the textbook. 3. Writing Programs. Recall that a program is just a set of instructions for the computer to execute. Let’s start with a basic command: print x: Prints the value of the expression x, followed by a newline. Program Text: print “Hello World!” print “How are you?” Output: Hello World! How are you? (The compiler needs those quotation marks to identify strings – we’ll talk about this later.) Don’t worry, we’ll be adding many more interesting commands later! For now, though, we’ll use this to have you become familiar with the administrative details of writing a program. Write a program that, when run, prints out a tic-tac-toe board. ­ Linux and OS X users: To write your program, just create a text file with the contents of the program text (e.g. the program text above.) To run your program (pretend its named first_print.py), use the command python first_print.py at the shell. ­ Windows users: The best way to program in Windows is using the IDLE GUI (from the shortcut above.) To create a new program, run the command File->New Window (CTRL+N) – this will open up a blank editing window. In general, save your program regularly; after saving, you can hit F5 to run your program and see the output. Make sure to write your program using the computer at least once! The purpose of this question is to make sure you know how to write programs using your computing environment; many students in introductory courses experience trouble with assignments not because they have trouble with the material but because of some weird environment quirk. You can write the answer to the question in the box below (I know its hard to show spaces while writing – just do your best) Program Text:
  • 3. Expected Output: | | ----- | | ----- | | 4. Interpreter Mode. Python has a write-as-you-go mode that’s useful when testing small snippets of code. You can access this by running the command python at the shell (for OS X and Linux) or by starting the IDLE GUI (for Windows) – you should see a >>> prompt. Try typing a print command and watch what happens. You’ll find that its much more convenient to solve a lot of the homework problems using the interpreter. When you want to write an actual, self-contained program (e.g. writing the game tic-tac-toe), follow the instructions in section 3 above. 5. Variables. Put simply, variables are containers for storing information. For example: Program Text: a = “Hello World!” print a Output: Hello World! The = sign is an assignment operator which says: assign the value “Hello World!” to the variable a. Program Text: a = “Hello World!” a = “and goodbye..” print a Output: and goodbye.. Taking this second example, the value of a after executing the first line above is “Hello World!”, and after the second line its “and goodbye..” (which is what gets printed)
  • 4. In Python, variables are designed to hold specific types of information. For example, after the first command above is executed, the variable a is associated with the string type. There are several types of information that can be stored: ­ Boolean. Variables of this type can be either True or False. ­ Integer. An integer is a number without a fractional part, e.g. -4, 5, 0, -3. ­ Float. Any rational number, e.g. 3.432. We won’t worry about floats for today. ­ String. Any sequence of characters. We’ll get more in-depth with strings later in the week. Python (and many other languages) are zealous about type information. The string “5” and integer 5 are completely different entities to Python, despite their similar appearance. You’ll see the importance of this in the next section. Write a program that stores the value 5 in a variable a and prints out the value of a, then stores the value 7 in a and prints out the value of a (4 lines.) Program Text: Expected Output: 5 7 6. Operators. Python has the ability to be used as a cheap, 5-dollar calculator. In particular, it supports basic mathematical operators: +, -, *, /. Program Text: a = 5 + 7 print a Output: 12 Variables can be used too. Program Text: a = 5 b = a + 7 print b
  • 5. Output: 12 Expressions can get fairly complex. Program Text: a = (3+4+21) / 7 b = (9*4) / (2+1) - 6 print (a*b)-(a+b) Output: 14 These operators work on numbers. Type information is important – the following expressions would result in an error. “Hello” / 123 “Hi” + 5 “5” + 7 The last one is especially important! Note that Python just sees that 5 as a string – it has no concept of it possibly being a number. Some of the operators that Python includes are ­ Addition, Subtraction, Multiplication. a+b, a-b, and a*b respectively. ­ Integer Division. a/b. Note that when division is performed with two integers, only the quotient is returned (the remainder is ignored.) Try typing print 13/6 into the interpreter ­ Exponentiation (ab). a ** b. Operators are evaluated using the standard order of operations. You can use parentheses to force certain operators to be evaluated first. Let’s also introduce one string operation. ­ Concatenation. a+b. Combines two strings into one. “Hel” + “lo” would yield “Hello” Another example of type coming into play! When Python sees a + b, it checks to see what type a and b are. If they are both strings then it concatenates the two; if they are both integers it adds them; if one is a string and the other is an integer, it returns an error. Write the output of the following lines of code (if an error would result, write error):
  • 6. print 13 + 6 Output: _______________________________________ print 2 ** 3 Output: _______________________________________ print 2 * (1 + 3) Output: _______________________________________ print 8 / 9 Output: _______________________________________ print “13” + “6” Output: _______________________________________ print “13” + 6 Output: _______________________________________ 7. Review. Here are the important concepts covered today. ­ What is a program? How does one write them. ­ Using the interpreter mode. ­ Variables: containers for storing information. Variables are typed – each variable only holds certain types of data (one might be able to store strings, another might be able to store numbers, etc.) ­ Operators: +, -, *, /, **, and parentheses.