SlideShare uma empresa Scribd logo
1 de 15
Baixar para ler offline
>_ Things Lab 
Some hours of Python
What is Python 
•A high-level programming language 
•Supports multiple paradigms: 
–Imperative (how-to) programming 
–Functional programming 
–Object-Oriented programming (OOP) 
–Procedural programming 
•CPython implementation is open source 
https://github.com/python/cpython
The Zen of Python (5/19) 
•Beautiful is better than ugly 
•Sparse is better than dense 
•Simple is better than complex 
•Complex is better than complicated 
•Errors should never pass silently
2or3? 
Python v2.x 
•Legacy of the language 
•Older version 
•Backward compatible 
•A lot of libraries, making it extendable 
Python v3.x 
•The present and the future 
•More updated than v2.x 
•Standard library improvements 
•Backwards incompatible
What comes next 
•Installing the Python (3.x) interpreter 
•Greet the console 
•Crunch some numbers 
•What if…? 
•Repeat, repeat, repeat!
Greet the console 
print("Hello") 
# This is a comment. We use the input() function 
# to get string input from the user 
user = input("What's your name? ") # reading the user's name 
# By using %s for strings, %d for ints and %f for FP, 
# we can pre-format strings 
print("Hello %s, it's so nice to meet you." % user) 
# We use the int() function to parse an integer out of a 
# string. In this case, we are parsing the input string 
age = int(input("How old are you? ")) 
print("So you were born in %d? That's nice" % (2014 - age)) 
print("I was born on 1991, so I'm 23 years old.") 
print("We can learn so much together")
Crunch some numbers 
import math 
radius = 5 
PI = math.pi 
print("Our circle of radius %d has:" % radius) 
print(" - Perimeter of %f" % (2*PI*radius)) 
print(" - Area of %f" % (PI * radius**2)) 
# # # # # # # # # # # # # # # # # # # # # # # # # 
a = float(input("Side 1 of triangle: ")) 
b = float(input("Side 2 of triangle: ")) 
c = float(input("Side 3 of triangle: ")) 
p = (a + b + c ) / 2 
area = math.sqrt(p * (p-a) * (p-b) * (p-c)) 
print("The area of the triangle is %f" % area)
Crunch some numbers: self test 
1.Fahrenheit to Celsius converter 
2.Feet to meters converter 
3.**Sum of digits of a number in [0; 1000] 
4.*Compute BMI 
5.*Calculate compound interest 
Don’t forget: GIYF (Google Is Your Friend)
What if…? 
import math 
print("ax^2 + bx + c = 0") 
a = float(input("a: ")) 
b = float(input("b: ")) 
c = float(input("c: ")) 
D = b**2 - 4*a*c 
if D > 0: 
# two real roots 
x1 = (-b + math.sqrt(D))/(2*a) 
x2 = (-b - math.sqrt(D))/(2*a) 
print("Two real roots:") 
print("x1 = %f" % x1) 
print("x2 = %f" % x2) 
elif D == 0: 
# one real root 
x = (-b)/(2*a) 
print("Only one real root:") 
print("x = %f" % x) 
else: 
print("No real roots")
What if…? 
if D > 0: 
# two real roots 
x1 = (-b + math.sqrt(D))/(2*a) 
x2 = (-b - math.sqrt(D))/(2*a) 
print("Two real roots:") 
print("x1 = %f" % x1) 
print("x2 = %f" % x2) 
elif D == 0: 
# one real root 
x = (-b)/(2*a) 
print("Only one real root:") 
print("x = %f" % x) 
else: 
print("No real roots")
What if you get yourself tested? 
1.*Compute and interpret BMI 
2.**Use Zeller’s congruence to calculate the day of the week for a given date 
3.*Rock, Scissor, Paper game 
4.Randomly pick two numbers < 100 and ask the user to add, subtract and multiply them. For every correct result, give him 1 mark. Print the marks in the end.
Repeat 
print("All multiples of 2 AND 3 between 0 and 99(100-1):") 
for i in range(100): 
if i%2 == 0 and i%3 == 0: # even 
print("%d " % i)
Repeat! 
from random import randint 
# pick a random integer 
number = randint(1,100) 
while True: 
guess = int(input("Guess the number: ")) 
if guess > number: 
print("Wrong! Your guess is too high!") 
elif guess < number: 
print("Wrong! Your guess is too low!") 
else: 
print("***CORRECT!***") 
break
Repeat more! 
1.Modify the above program to count how many steps did it require to guess the correct number. 
2.*Find the sum of digits of any number 
3.*Display all prime numbers between 2 and 1000
Final project 
•Write a mathematical test. The test should ask the user for their level. Based on the level, it should ask arithmetic question regarding addition, subtraction and multiplication (chose by the user). In the end, it should give the result of the test, which is “Well Done” for more than 75%, “Good” for more than 50%, and “Bad” for less than 50%. For the Beginner level, the numbers should be 10, for Medium 50, and for Advanced 100.

Mais conteúdo relacionado

Semelhante a Some hours of python

Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 

Semelhante a Some hours of python (20)

Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Cc code cards
Cc code cardsCc code cards
Cc code cards
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIML
 
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programs
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
 
Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructions
 
CODING QUESTIONS.pptx
CODING QUESTIONS.pptxCODING QUESTIONS.pptx
CODING QUESTIONS.pptx
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
Python crush course
Python crush coursePython crush course
Python crush course
 
Something about Golang
Something about GolangSomething about Golang
Something about Golang
 

Mais de Things Lab

Source Code Management with Git
Source Code Management with GitSource Code Management with Git
Source Code Management with Git
Things Lab
 

Mais de Things Lab (14)

3D Printer Workshop - From your idea to a real object
3D Printer Workshop - From your idea to a real object3D Printer Workshop - From your idea to a real object
3D Printer Workshop - From your idea to a real object
 
Things lab - Intro fritzing
Things lab - Intro fritzingThings lab - Intro fritzing
Things lab - Intro fritzing
 
Things lab - introduction to programming
Things lab - introduction to programmingThings lab - introduction to programming
Things lab - introduction to programming
 
Real world Webapp
Real world WebappReal world Webapp
Real world Webapp
 
Rapid Prototyping
Rapid PrototypingRapid Prototyping
Rapid Prototyping
 
Website with HTML CSS
Website with HTML CSSWebsite with HTML CSS
Website with HTML CSS
 
(Not so) big data with Chart.js
(Not so) big data with Chart.js(Not so) big data with Chart.js
(Not so) big data with Chart.js
 
Arduino
ArduinoArduino
Arduino
 
Cryptanalysis - basic ciphers and a bit more
Cryptanalysis - basic ciphers and a bit moreCryptanalysis - basic ciphers and a bit more
Cryptanalysis - basic ciphers and a bit more
 
PHP and Databases
PHP and DatabasesPHP and Databases
PHP and Databases
 
PHP Programming: Intro
PHP Programming: IntroPHP Programming: Intro
PHP Programming: Intro
 
Source Code Management with Git
Source Code Management with GitSource Code Management with Git
Source Code Management with Git
 
An Hour of Arduino and Ardublock
An Hour of Arduino and ArdublockAn Hour of Arduino and Ardublock
An Hour of Arduino and Ardublock
 
Databases and MySQL
Databases and MySQLDatabases and MySQL
Databases and MySQL
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Some hours of python

  • 1. >_ Things Lab Some hours of Python
  • 2. What is Python •A high-level programming language •Supports multiple paradigms: –Imperative (how-to) programming –Functional programming –Object-Oriented programming (OOP) –Procedural programming •CPython implementation is open source https://github.com/python/cpython
  • 3. The Zen of Python (5/19) •Beautiful is better than ugly •Sparse is better than dense •Simple is better than complex •Complex is better than complicated •Errors should never pass silently
  • 4. 2or3? Python v2.x •Legacy of the language •Older version •Backward compatible •A lot of libraries, making it extendable Python v3.x •The present and the future •More updated than v2.x •Standard library improvements •Backwards incompatible
  • 5. What comes next •Installing the Python (3.x) interpreter •Greet the console •Crunch some numbers •What if…? •Repeat, repeat, repeat!
  • 6. Greet the console print("Hello") # This is a comment. We use the input() function # to get string input from the user user = input("What's your name? ") # reading the user's name # By using %s for strings, %d for ints and %f for FP, # we can pre-format strings print("Hello %s, it's so nice to meet you." % user) # We use the int() function to parse an integer out of a # string. In this case, we are parsing the input string age = int(input("How old are you? ")) print("So you were born in %d? That's nice" % (2014 - age)) print("I was born on 1991, so I'm 23 years old.") print("We can learn so much together")
  • 7. Crunch some numbers import math radius = 5 PI = math.pi print("Our circle of radius %d has:" % radius) print(" - Perimeter of %f" % (2*PI*radius)) print(" - Area of %f" % (PI * radius**2)) # # # # # # # # # # # # # # # # # # # # # # # # # a = float(input("Side 1 of triangle: ")) b = float(input("Side 2 of triangle: ")) c = float(input("Side 3 of triangle: ")) p = (a + b + c ) / 2 area = math.sqrt(p * (p-a) * (p-b) * (p-c)) print("The area of the triangle is %f" % area)
  • 8. Crunch some numbers: self test 1.Fahrenheit to Celsius converter 2.Feet to meters converter 3.**Sum of digits of a number in [0; 1000] 4.*Compute BMI 5.*Calculate compound interest Don’t forget: GIYF (Google Is Your Friend)
  • 9. What if…? import math print("ax^2 + bx + c = 0") a = float(input("a: ")) b = float(input("b: ")) c = float(input("c: ")) D = b**2 - 4*a*c if D > 0: # two real roots x1 = (-b + math.sqrt(D))/(2*a) x2 = (-b - math.sqrt(D))/(2*a) print("Two real roots:") print("x1 = %f" % x1) print("x2 = %f" % x2) elif D == 0: # one real root x = (-b)/(2*a) print("Only one real root:") print("x = %f" % x) else: print("No real roots")
  • 10. What if…? if D > 0: # two real roots x1 = (-b + math.sqrt(D))/(2*a) x2 = (-b - math.sqrt(D))/(2*a) print("Two real roots:") print("x1 = %f" % x1) print("x2 = %f" % x2) elif D == 0: # one real root x = (-b)/(2*a) print("Only one real root:") print("x = %f" % x) else: print("No real roots")
  • 11. What if you get yourself tested? 1.*Compute and interpret BMI 2.**Use Zeller’s congruence to calculate the day of the week for a given date 3.*Rock, Scissor, Paper game 4.Randomly pick two numbers < 100 and ask the user to add, subtract and multiply them. For every correct result, give him 1 mark. Print the marks in the end.
  • 12. Repeat print("All multiples of 2 AND 3 between 0 and 99(100-1):") for i in range(100): if i%2 == 0 and i%3 == 0: # even print("%d " % i)
  • 13. Repeat! from random import randint # pick a random integer number = randint(1,100) while True: guess = int(input("Guess the number: ")) if guess > number: print("Wrong! Your guess is too high!") elif guess < number: print("Wrong! Your guess is too low!") else: print("***CORRECT!***") break
  • 14. Repeat more! 1.Modify the above program to count how many steps did it require to guess the correct number. 2.*Find the sum of digits of any number 3.*Display all prime numbers between 2 and 1000
  • 15. Final project •Write a mathematical test. The test should ask the user for their level. Based on the level, it should ask arithmetic question regarding addition, subtraction and multiplication (chose by the user). In the end, it should give the result of the test, which is “Well Done” for more than 75%, “Good” for more than 50%, and “Bad” for less than 50%. For the Beginner level, the numbers should be 10, for Medium 50, and for Advanced 100.