SlideShare a Scribd company logo
1 of 8
1.2 Basics of Python
1ARULKUMAR V AP/CSE - SECE
Motivations
Suppose, for example, that you need to take out a
student loan. Given the loan amount, loan term,
and annual interest rate, can you write a program
to compute the monthly payment and total
payment? This chapter shows you how to write
programs like this. Along the way, you learn the
basic steps that go into analyzing a problem,
designing a solution, and implementing the
solution by creating a program.
2ARULKUMAR V AP/CSE - SECE
Objectives
• To write programs that perform simple computations (§2.2).
• To obtain input from a program’s user by using the input function
(§2.3).
• To use identifiers to name variables (§2.4).
• To assign data to variables (§2.5).
• To define named constants (§2.6).
• To use the operators +, -, *, /, //, %, and ** (§2.7).
• To write and evaluate numeric expressions (§2.8).
• To use augmented assignment operators to simplify coding (§2.9).
• To perform numeric type conversion and rounding with the int and
round functions (§2.10).
• To obtain the current system time by using time.time() (§2.11).
• To describe the software development process and apply it to
develop the loan payment program (§2.12).
• To compute and display the distance between two points (§2.13).
3ARULKUMAR V AP/CSE - SECE
Introducing Programming with an
Example
Listing 2.1 Computing the Area of a
Circle
This program computes the area of
the circle.
4
ComputeArea
Run
IMPORTANT NOTE:
(1) To enable the buttons, you must download the entire slide
file slide.zip and unzip the files into a directory (e.g.,
c:slide). (2) You must have installed Python and set python
bin directory in the environment path. (3) If you are using
Office 2010, check PowerPoint2010.doc located in the
same folder with this ppt file.
ARULKUMAR V AP/CSE - SECE
Trace a Program Execution
# Assign a radius
radius = 20 # radius is now 20
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius " +
str(radius) + " is " + str(area))
5
20radius
Assign 20 to
radius
animation
ARULKUMAR V AP/CSE - SECE
Trace a Program Execution
# Assign a radius
radius = 20 # radius is now 20
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius“,
radius, " is "area)
6
20radius
Assign result to
area
animation
1256.636area
ARULKUMAR V AP/CSE - SECE
Trace a Program Execution
# Assign a radius
radius = 20 # radius is now 20
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius",
radius, "is", area)
7
20radius
print a message to
the console
animation
1256.636area
ARULKUMAR V AP/CSE - SECE
Reading Input from the Console
8
1. Use the input function
variable = input("Enter a string: ")
2. Use the eval function
var = eval(stringVariable)
eval("51 + (54 * (3 + 2))") returns 321.
ComputeAreaWithConsoleInput
Run
ComputeAverage
Run
ARULKUMAR V AP/CSE - SECE

More Related Content

Similar to 1. basics of python

Cw comp1640 211453_mo233_20131120_214054_1314
Cw comp1640 211453_mo233_20131120_214054_1314Cw comp1640 211453_mo233_20131120_214054_1314
Cw comp1640 211453_mo233_20131120_214054_1314Owen Muzi
 
Biam 560 Enhance teaching / snaptutorial.com
Biam 560  Enhance teaching / snaptutorial.comBiam 560  Enhance teaching / snaptutorial.com
Biam 560 Enhance teaching / snaptutorial.comHarrisGeorg45
 
Cmgt 410 cmgt410 cmgt 410 education for service uopstudy.com
Cmgt 410 cmgt410 cmgt 410 education for service   uopstudy.comCmgt 410 cmgt410 cmgt 410 education for service   uopstudy.com
Cmgt 410 cmgt410 cmgt 410 education for service uopstudy.comUOPCourseHelp
 
Be cps-18 cps13or23-module1
Be cps-18 cps13or23-module1Be cps-18 cps13or23-module1
Be cps-18 cps13or23-module1kavya R
 
Engineering C-programing module1 ppt (18CPS13/23)
Engineering C-programing module1 ppt (18CPS13/23)Engineering C-programing module1 ppt (18CPS13/23)
Engineering C-programing module1 ppt (18CPS13/23)kavya R
 
© SAP SE CASE STUDY .docx
 © SAP SE   CASE STUDY .docx © SAP SE   CASE STUDY .docx
© SAP SE CASE STUDY .docxmayank272369
 
Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6comp274
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6solutionjug4
 
02slide_accessible.pptx
02slide_accessible.pptx02slide_accessible.pptx
02slide_accessible.pptxMattMarino13
 
System programmin practical file
System programmin practical fileSystem programmin practical file
System programmin practical fileAnkit Dixit
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6helpido9
 
Primitive Data Types and Operations
Primitive Data Types and OperationsPrimitive Data Types and Operations
Primitive Data Types and Operationsnaimanighat
 
Cis 498 Inspiring Innovation--tutorialrank.com
Cis 498  Inspiring Innovation--tutorialrank.comCis 498  Inspiring Innovation--tutorialrank.com
Cis 498 Inspiring Innovation--tutorialrank.comPrescottLunt371
 
COCOMO methods for software size estimation
COCOMO methods for software size estimationCOCOMO methods for software size estimation
COCOMO methods for software size estimationPramod Parajuli
 
Unit3 overview of_c_programming
Unit3 overview of_c_programmingUnit3 overview of_c_programming
Unit3 overview of_c_programmingCapuchino HuiNing
 
An Inter-Wiki Page Data Processor for a M2M System @Matsue, 1sep., Eskm2013
An Inter-Wiki Page Data Processor for a M2M System  @Matsue, 1sep., Eskm2013An Inter-Wiki Page Data Processor for a M2M System  @Matsue, 1sep., Eskm2013
An Inter-Wiki Page Data Processor for a M2M System @Matsue, 1sep., Eskm2013Takashi Yamanoue
 
Term 2 CS Practical File 2021-22.pdf
Term 2 CS Practical File 2021-22.pdfTerm 2 CS Practical File 2021-22.pdf
Term 2 CS Practical File 2021-22.pdfKiranKumari204016
 
44259126 ms-project-presentation
44259126 ms-project-presentation44259126 ms-project-presentation
44259126 ms-project-presentationFarrukh Raja
 

Similar to 1. basics of python (20)

Cw comp1640 211453_mo233_20131120_214054_1314
Cw comp1640 211453_mo233_20131120_214054_1314Cw comp1640 211453_mo233_20131120_214054_1314
Cw comp1640 211453_mo233_20131120_214054_1314
 
Biam 560 Enhance teaching / snaptutorial.com
Biam 560  Enhance teaching / snaptutorial.comBiam 560  Enhance teaching / snaptutorial.com
Biam 560 Enhance teaching / snaptutorial.com
 
Cmgt 410 cmgt410 cmgt 410 education for service uopstudy.com
Cmgt 410 cmgt410 cmgt 410 education for service   uopstudy.comCmgt 410 cmgt410 cmgt 410 education for service   uopstudy.com
Cmgt 410 cmgt410 cmgt 410 education for service uopstudy.com
 
Be cps-18 cps13or23-module1
Be cps-18 cps13or23-module1Be cps-18 cps13or23-module1
Be cps-18 cps13or23-module1
 
Engineering C-programing module1 ppt (18CPS13/23)
Engineering C-programing module1 ppt (18CPS13/23)Engineering C-programing module1 ppt (18CPS13/23)
Engineering C-programing module1 ppt (18CPS13/23)
 
LMmanual.pdf
LMmanual.pdfLMmanual.pdf
LMmanual.pdf
 
© SAP SE CASE STUDY .docx
 © SAP SE   CASE STUDY .docx © SAP SE   CASE STUDY .docx
© SAP SE CASE STUDY .docx
 
Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6
 
02slide_accessible.pptx
02slide_accessible.pptx02slide_accessible.pptx
02slide_accessible.pptx
 
System programmin practical file
System programmin practical fileSystem programmin practical file
System programmin practical file
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6
 
Primitive Data Types and Operations
Primitive Data Types and OperationsPrimitive Data Types and Operations
Primitive Data Types and Operations
 
Cis 498 Inspiring Innovation--tutorialrank.com
Cis 498  Inspiring Innovation--tutorialrank.comCis 498  Inspiring Innovation--tutorialrank.com
Cis 498 Inspiring Innovation--tutorialrank.com
 
COCOMO methods for software size estimation
COCOMO methods for software size estimationCOCOMO methods for software size estimation
COCOMO methods for software size estimation
 
Unit3 overview of_c_programming
Unit3 overview of_c_programmingUnit3 overview of_c_programming
Unit3 overview of_c_programming
 
PRELIM-Lesson-2.pdf
PRELIM-Lesson-2.pdfPRELIM-Lesson-2.pdf
PRELIM-Lesson-2.pdf
 
An Inter-Wiki Page Data Processor for a M2M System @Matsue, 1sep., Eskm2013
An Inter-Wiki Page Data Processor for a M2M System  @Matsue, 1sep., Eskm2013An Inter-Wiki Page Data Processor for a M2M System  @Matsue, 1sep., Eskm2013
An Inter-Wiki Page Data Processor for a M2M System @Matsue, 1sep., Eskm2013
 
Term 2 CS Practical File 2021-22.pdf
Term 2 CS Practical File 2021-22.pdfTerm 2 CS Practical File 2021-22.pdf
Term 2 CS Practical File 2021-22.pdf
 
44259126 ms-project-presentation
44259126 ms-project-presentation44259126 ms-project-presentation
44259126 ms-project-presentation
 

More from PhD Research Scholar (20)

Ajax
AjaxAjax
Ajax
 
Quiz servlet
Quiz servletQuiz servlet
Quiz servlet
 
Quiz
QuizQuiz
Quiz
 
servlet db connectivity
servlet db connectivityservlet db connectivity
servlet db connectivity
 
2.java script dom
2.java script  dom2.java script  dom
2.java script dom
 
1.java script
1.java script1.java script
1.java script
 
Quiz javascript
Quiz javascriptQuiz javascript
Quiz javascript
 
Thread&multithread
Thread&multithreadThread&multithread
Thread&multithread
 
String
StringString
String
 
Streams&io
Streams&ioStreams&io
Streams&io
 
Packages
PackagesPackages
Packages
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Exception handling
Exception handlingException handling
Exception handling
 
Applets
AppletsApplets
Applets
 
Abstract class
Abstract classAbstract class
Abstract class
 
7. tuples, set & dictionary
7. tuples, set & dictionary7. tuples, set & dictionary
7. tuples, set & dictionary
 
6. list
6. list6. list
6. list
 
5. string
5. string5. string
5. string
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 

1. basics of python

  • 1. 1.2 Basics of Python 1ARULKUMAR V AP/CSE - SECE
  • 2. Motivations Suppose, for example, that you need to take out a student loan. Given the loan amount, loan term, and annual interest rate, can you write a program to compute the monthly payment and total payment? This chapter shows you how to write programs like this. Along the way, you learn the basic steps that go into analyzing a problem, designing a solution, and implementing the solution by creating a program. 2ARULKUMAR V AP/CSE - SECE
  • 3. Objectives • To write programs that perform simple computations (§2.2). • To obtain input from a program’s user by using the input function (§2.3). • To use identifiers to name variables (§2.4). • To assign data to variables (§2.5). • To define named constants (§2.6). • To use the operators +, -, *, /, //, %, and ** (§2.7). • To write and evaluate numeric expressions (§2.8). • To use augmented assignment operators to simplify coding (§2.9). • To perform numeric type conversion and rounding with the int and round functions (§2.10). • To obtain the current system time by using time.time() (§2.11). • To describe the software development process and apply it to develop the loan payment program (§2.12). • To compute and display the distance between two points (§2.13). 3ARULKUMAR V AP/CSE - SECE
  • 4. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area of the circle. 4 ComputeArea Run IMPORTANT NOTE: (1) To enable the buttons, you must download the entire slide file slide.zip and unzip the files into a directory (e.g., c:slide). (2) You must have installed Python and set python bin directory in the environment path. (3) If you are using Office 2010, check PowerPoint2010.doc located in the same folder with this ppt file. ARULKUMAR V AP/CSE - SECE
  • 5. Trace a Program Execution # Assign a radius radius = 20 # radius is now 20 # Compute area area = radius * radius * 3.14159 # Display results print("The area for the circle of radius " + str(radius) + " is " + str(area)) 5 20radius Assign 20 to radius animation ARULKUMAR V AP/CSE - SECE
  • 6. Trace a Program Execution # Assign a radius radius = 20 # radius is now 20 # Compute area area = radius * radius * 3.14159 # Display results print("The area for the circle of radius“, radius, " is "area) 6 20radius Assign result to area animation 1256.636area ARULKUMAR V AP/CSE - SECE
  • 7. Trace a Program Execution # Assign a radius radius = 20 # radius is now 20 # Compute area area = radius * radius * 3.14159 # Display results print("The area for the circle of radius", radius, "is", area) 7 20radius print a message to the console animation 1256.636area ARULKUMAR V AP/CSE - SECE
  • 8. Reading Input from the Console 8 1. Use the input function variable = input("Enter a string: ") 2. Use the eval function var = eval(stringVariable) eval("51 + (54 * (3 + 2))") returns 321. ComputeAreaWithConsoleInput Run ComputeAverage Run ARULKUMAR V AP/CSE - SECE