SlideShare a Scribd company logo
1 of 25
Let’s really start
Python SIG – PYA
Class 2 – 29/9/15
(Revision of) User input
• Complex programs = input + efficient
computation + output
• raw_input, NOT input
• raw_input – reads as string, so convert when
required (strongly typed, remember?)
• Example:
– var = raw_input(prompt)
‘print’ing
• print statements, parentheses in Py3;
• But backported to 2; so print() works
• Pretty flexible: print some_string, var works
• Automatically adds newline
• Use comma to suppress
Useful built-ins
• len()
• range() (and xrange())
• sum(), max(), min()
• int(), float(), str()
• dir() – use it to find list and string methods
• Use help() to find out about them
(again) Read the documentation
• Online / Offline
• Google
• Stack Overflow
• (xkcd?)
• “Don’t have to memorise, only know how to
find what you need.”
- Me (in last class)
Obligatory xkcd reference[1]
(Revision of)Boolean
• True / False (capitalization matters!)
• >
• <
• ==
• !=
• >=
• <=
• is , is not (same as id())
if else elif
if SomethingThatEvaluatesToABoolean:
# code
elif SomethingElseThatEvaluatesToABoolean:
# code
else:
# code
Other if structures
• nested if
• elif ladders
• Basically “forking the flow of program”
• Hence called control flow
(Revision of)Strings
• Immutable
• Single or double, they don’t care
• Raw strings
• Escape sequences
• String slicing – string[start:stop:step]
• We count from 0 (why?)
• String methods
String formatting
• What is it? (Immutable)
• %something - %d, %f, %s
– ‘a = %d’ % (a)
• Prefer .format()
• {} sufficient – numbers optional
• More complex things possible
In-class assignment
Write a program that reads two numbers from
the user, (converting them to integers if they
are floating point numbers) and then prints
their sum along with the user input in this
format:
‘enter number 1: ’ 5.67
‘enter number 2: ’ 5.67
‘You entered 5.67 and 5.67. Adding 5 and
5, sum is 10.’
In-class assignment[2]
Write a program that takes input of the form
‘num1,num2,[any one of +-*/]’ as a string
and performs the given operation. In other
words, your program is a simple calculator
which can perform floating point arithmetic.
Try making improvements like asking for no.
of decimal digits to be displayed.
Solution required?
Explanation of solutions
• Essentially it is branching – splitting paths
• Be careful with ‘else’ – executes
unconditionally
• That’s why error handling – ‘try’
• Interesting fact – anything you can code, can
be coded with whatever you know now!
• (But incredibly tedious)
Why loops?
• Essential part of making our programs more
complex.
• When do you use for loops?
• When do you use while loops?
• If (heh.) the while loop is a ‘moving’ if,
what is the for loop?
Loops
• for – for when you how many iterations
• while - while you don’t know how many
iterations
• while SomethingThatEvaluatesToABoolean:
# code
• for loop syntax in Python
– for iterVar in (x)range(iterNum):
# code
for loops can do more!
• What is this ‘in’ anyway? (different time
complexity for different cases)
• for char in string
• for line in text
• for item in sequence
–Example: for i in [1,’a’,3]:
print i
# output: 1nan3
Confused / Want to know more?
Let’s do help(‘for’) and see what
we can make of it!
Loop shortcuts:
• break – jumps out of nearest loop
• continue – skips to top of nearest loop
• In what situation can break be used?
• In what situation can continue be used?
In-class assignment
• Input an angle as degrees. And find a
trigonometric ratio based on users choice[out of
sin, cos and tan]. Do this ‘n’ times, where ‘n’ was
entered by the user initially.
HINT : import math. (what is import,
from..import .. ?) [3]
• Input two strings. One takes in login_ID and
other password. It prints “login_ID is successful”
if password is password. Else should show a fail
message and keep prompting user for
password.[3]
In-class assignment
Find the factorial of a given number. Don’t
import math. Try to make your program break
and fix all those cases. (recursive vs.
iterative)[2]
Note that I said ‘number’. What if it user enters
a string and then it leads to a TypeError?
To those who have finished:
• Try to make your calculator (assignment from
earlier today) mimic a real life calculator more.
• That is, make it capable of storing the result of
a calculation and asking the user what
operation to do with that, and so on; until the
user types ‘end’ or something.
Thanks!
Pranav S Bijapur,
ECE, BMSCE, Bangalore
b.pranavshankar@gmail.com
Telegram - @pranavsb
References
• All “(Revision of)” slides were taken from class
1 presentation made by me on 11-9-15
• [1] – xkcd webcomic - https://xkcd.com/1425/
• [2] – idea by Tarun Verma
• [3] – modified form of something Tarun Verma
said

More Related Content

What's hot

CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHMCLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHMRc Os
 
Dynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingDynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingYusuke Miyazaki
 
Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1Ali Aminian
 
Ch3 Formatted Input/Output
Ch3 Formatted Input/OutputCh3 Formatted Input/Output
Ch3 Formatted Input/OutputSzeChingChen
 
Intro To BOOST.Spirit
Intro To BOOST.SpiritIntro To BOOST.Spirit
Intro To BOOST.SpiritWill Shen
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventionssaranyatdr
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection StatementsSzeChingChen
 
Java Tutorial: Part 4 - Data and Calculations
Java Tutorial: Part 4 - Data and CalculationsJava Tutorial: Part 4 - Data and Calculations
Java Tutorial: Part 4 - Data and CalculationsSvetlin Nakov
 

What's hot (18)

Thin Template Explained
Thin Template ExplainedThin Template Explained
Thin Template Explained
 
C programming part4
C programming part4C programming part4
C programming part4
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHMCLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 
Dynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner TypingDynamic Type Inference for Gradual Hindley–Milner Typing
Dynamic Type Inference for Gradual Hindley–Milner Typing
 
Ch8 Arrays
Ch8 ArraysCh8 Arrays
Ch8 Arrays
 
Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1
 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
 
Ch3 Formatted Input/Output
Ch3 Formatted Input/OutputCh3 Formatted Input/Output
Ch3 Formatted Input/Output
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Intro To BOOST.Spirit
Intro To BOOST.SpiritIntro To BOOST.Spirit
Intro To BOOST.Spirit
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventions
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
20130523 05 - Cyclomatic complexity
20130523 05 - Cyclomatic complexity20130523 05 - Cyclomatic complexity
20130523 05 - Cyclomatic complexity
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
Strings
StringsStrings
Strings
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
 
Java Tutorial: Part 4 - Data and Calculations
Java Tutorial: Part 4 - Data and CalculationsJava Tutorial: Part 4 - Data and Calculations
Java Tutorial: Part 4 - Data and Calculations
 

Similar to if, while and for in Python

Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methodsPranavSB
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Amr Alaa El Deen
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptracha49
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptBhargaviDalal4
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit onessuserb7c8b8
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdflailoesakhan
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Module 1 - Programming Fundamentals.pptx
Module 1 - Programming Fundamentals.pptxModule 1 - Programming Fundamentals.pptx
Module 1 - Programming Fundamentals.pptxGaneshRaghu4
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageAzilen Technologies Pvt. Ltd.
 
BCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.pptBCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.pptKirti Verma
 
Problem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialProblem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialTo Sum It Up
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh MalothBhavsingh Maloth
 

Similar to if, while and for in Python (20)

Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit one
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Module 1 - Programming Fundamentals.pptx
Module 1 - Programming Fundamentals.pptxModule 1 - Programming Fundamentals.pptx
Module 1 - Programming Fundamentals.pptx
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 
BCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.pptBCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.ppt
 
Problem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialProblem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study material
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
 
Python
PythonPython
Python
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 

Recently uploaded

GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 

Recently uploaded (20)

GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 

if, while and for in Python

  • 1. Let’s really start Python SIG – PYA Class 2 – 29/9/15
  • 2. (Revision of) User input • Complex programs = input + efficient computation + output • raw_input, NOT input • raw_input – reads as string, so convert when required (strongly typed, remember?) • Example: – var = raw_input(prompt)
  • 3. ‘print’ing • print statements, parentheses in Py3; • But backported to 2; so print() works • Pretty flexible: print some_string, var works • Automatically adds newline • Use comma to suppress
  • 4. Useful built-ins • len() • range() (and xrange()) • sum(), max(), min() • int(), float(), str() • dir() – use it to find list and string methods • Use help() to find out about them
  • 5. (again) Read the documentation • Online / Offline • Google • Stack Overflow • (xkcd?) • “Don’t have to memorise, only know how to find what you need.” - Me (in last class)
  • 7. (Revision of)Boolean • True / False (capitalization matters!) • > • < • == • != • >= • <= • is , is not (same as id())
  • 8. if else elif if SomethingThatEvaluatesToABoolean: # code elif SomethingElseThatEvaluatesToABoolean: # code else: # code
  • 9. Other if structures • nested if • elif ladders • Basically “forking the flow of program” • Hence called control flow
  • 10. (Revision of)Strings • Immutable • Single or double, they don’t care • Raw strings • Escape sequences • String slicing – string[start:stop:step] • We count from 0 (why?) • String methods
  • 11. String formatting • What is it? (Immutable) • %something - %d, %f, %s – ‘a = %d’ % (a) • Prefer .format() • {} sufficient – numbers optional • More complex things possible
  • 12. In-class assignment Write a program that reads two numbers from the user, (converting them to integers if they are floating point numbers) and then prints their sum along with the user input in this format: ‘enter number 1: ’ 5.67 ‘enter number 2: ’ 5.67 ‘You entered 5.67 and 5.67. Adding 5 and 5, sum is 10.’
  • 13. In-class assignment[2] Write a program that takes input of the form ‘num1,num2,[any one of +-*/]’ as a string and performs the given operation. In other words, your program is a simple calculator which can perform floating point arithmetic. Try making improvements like asking for no. of decimal digits to be displayed.
  • 15. Explanation of solutions • Essentially it is branching – splitting paths • Be careful with ‘else’ – executes unconditionally • That’s why error handling – ‘try’ • Interesting fact – anything you can code, can be coded with whatever you know now! • (But incredibly tedious)
  • 16. Why loops? • Essential part of making our programs more complex. • When do you use for loops? • When do you use while loops? • If (heh.) the while loop is a ‘moving’ if, what is the for loop?
  • 17. Loops • for – for when you how many iterations • while - while you don’t know how many iterations • while SomethingThatEvaluatesToABoolean: # code • for loop syntax in Python – for iterVar in (x)range(iterNum): # code
  • 18. for loops can do more! • What is this ‘in’ anyway? (different time complexity for different cases) • for char in string • for line in text • for item in sequence –Example: for i in [1,’a’,3]: print i # output: 1nan3
  • 19. Confused / Want to know more? Let’s do help(‘for’) and see what we can make of it!
  • 20. Loop shortcuts: • break – jumps out of nearest loop • continue – skips to top of nearest loop • In what situation can break be used? • In what situation can continue be used?
  • 21. In-class assignment • Input an angle as degrees. And find a trigonometric ratio based on users choice[out of sin, cos and tan]. Do this ‘n’ times, where ‘n’ was entered by the user initially. HINT : import math. (what is import, from..import .. ?) [3] • Input two strings. One takes in login_ID and other password. It prints “login_ID is successful” if password is password. Else should show a fail message and keep prompting user for password.[3]
  • 22. In-class assignment Find the factorial of a given number. Don’t import math. Try to make your program break and fix all those cases. (recursive vs. iterative)[2] Note that I said ‘number’. What if it user enters a string and then it leads to a TypeError?
  • 23. To those who have finished: • Try to make your calculator (assignment from earlier today) mimic a real life calculator more. • That is, make it capable of storing the result of a calculation and asking the user what operation to do with that, and so on; until the user types ‘end’ or something.
  • 24. Thanks! Pranav S Bijapur, ECE, BMSCE, Bangalore b.pranavshankar@gmail.com Telegram - @pranavsb
  • 25. References • All “(Revision of)” slides were taken from class 1 presentation made by me on 11-9-15 • [1] – xkcd webcomic - https://xkcd.com/1425/ • [2] – idea by Tarun Verma • [3] – modified form of something Tarun Verma said