Python Tutorial - 3. Numbers

codebasics · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

This video tutorial covers the basics of numbers in Python, including simple mathematical operations, functions, and the order of operations. It demonstrates how to use Python as a calculator, perform arithmetic operations, and work with integers and floats.

Full Transcript

hello today's topic is numbers in Python numbers are very important in gradient of any program I cannot think of any software program in the world which doesn't use numbers so they're extremely important now in Python you can use Python programming language as a calculator so let me demonstrate that to you by doing some simple mathem iCal operations so for example you want to do addition of these two numbers you can do it like this so just type in two numbers and plus sign in between if you want to do subtraction you can do subtraction you can do division like this so for division you will use this SL shine if you want to do multiplication you will use star sign and it will multiply two numbers now if you want to find a remainder of uh a division then you will use a percentage sign so for example I want to divide 11 by 2 and I want to find out the remainder for that you will use percentage sign like this and you can guess what will be the answer okay I think you guessed it right so 11/ 2 is like 2 into 5 is 10 and then remainder will be one you can also find the power of any number so for example you want to find 3 to the power of two then the way you do that is by using two star sign 2 times so it will do this so 3 to ^ of 2 is 9 Guess What will be the answer of this this is 2 to the power of four 16 okay so when you are doing any number manipulation when you're writing any Python program uh normally you would create variables to store these numbers and you will do these mathematical operations on those variables okay so let me let me demonstrate this by having a simple problem so for example you want to travel from New York to Baltimore and then from Baltimore to Pittsburgh and you know the distance between these cities now you want to find out the total distance so the way you do it is you first create a variable to store the distance between New York City to Baltimore let's say it is 188 miles so I just created a variable called this and assign value 188 to it then you create another variable to store the distance between Baltimore to Pittsburgh and let's say this is 247 miles okay so to find out the total distance you create another variable called total distance and you just assign those first two variables to it so when I do like this and if I print the total distance it will tell me the total distance that I will be traveling during my journey okay another interesting problem could be let's say if you are driving at 65 miles per hour and you want to find the total time that you will spend in your journey and we're just assuming you're not taking any breaks you're like a super human being you don't need any brakes you can just constantly Drive okay so in that case again create another variable for your miles per hour speed so mph is equal to 65 this is your miles per hour and to find out the time you create another variable called time and say time equal to total distance if you have total distance and if you have miles per hour how do you find out the time right it's a simple math it is distance divided by the speed so distance divided by Miles hour is your time it's just a simple high school math or school math So when you say time it prints this number so you see um it's 6.69 hours now it is printing it with a lot higher decimal Precision you see after dot you see so many many decimal points if you want to see like just for Simplicity if you want to see like two decimal points you can use this round function so in Python there is this function called round where the first argument is as you see it in the tool tipe it's a number so your number is time and how many decimal points you want to see so you can say two so it is showing it as you see like after do you now see only two decimal points 1 two if you want to see like three decimal point Precision you can do this and it will show you three decimal points okay so just to summarize you can do all the basic mathematical operations in Python using these operator signs also there are two kind of numbers in Python one is integer which is like a whole number so for example 435 16 2 2 4 all these are integer numbers because they don't have a fractional part to it whereas this number is called a float number because it has a fractional part it's not a whole number basically it has a fraction to it so just remember this thing that two types of numbers integers and floats all right now let me show you something uh interesting so if I do let's say if I want to add 10 if I do this kind of operation uh just take a pause and uh think about what will be the answer what what will be the expected output okay all right so I'm just about to hit enter and you will see in a moment what it it prints now what were you expecting some of you might be expecting that it will do 10 + 2 which is 12 and then it will multiply it by 3 so 12 into 3 will be 36 but that's not what it printed why well the reason is the way python works is it has a priority on certain mathematical operations so for example in this case it will do 2 * 3 first so 2 * 3 is 6 and then it will add it to 10 so 10 + 6 is 16 if you want to do what you expected which is 10 + 2 and then multiply by 3 you need to use brackets so opening bracket 10 + 2 closing bracket cross 3 so by providing these brackets you are telling your programming platform form that I want the mathematical operation to happen in this order 10 + 2 and then three now you get 36 okay cool now let me show you one more interesting thing so 6 - 5.7 again take a pause and think what will be the answer it's really easy to Guess that right it should be 0.3 but let's see what happens oohoo why the hell I got this answer why is it not 0.3 well there is an answer so in any programming language when you try to store floating number It ultimately numbers are stored in binary format which is a sequence of 0o and one and the way protein numbers are stored they always lose the accuracy so there is no prec precise way to store floating numbers hence you will get this kind of behavior and this is not just with python this is with any programming languages and it's expected so it's not a bug or anything it is an expected output so if you want to again see3 then use rounding number round rounding function so just say around this particular number by two decimal points and you get 0.3 okay so that was all about numbers thanks for watching

Original Description

This python tutorial gives you an understanding of numbers. It will teach the use of numbers in python, perform simple mathematical operations, functions in python and order of operation. Topics that are covered in this Python Video: 0:00 Introduction 4:49 round() function Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Video without background music: https://youtu.be/pd2_ZbhFT38 Visit http://codebasicshub.com/tutorial/python/numbers-1 to download answers on exercises. Next Video: Python Tutorial - 4. Strings: https://www.youtube.com/watch?v=PsoKq0p5meU&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=5 Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 3 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

This video tutorial teaches the basics of numbers in Python, including simple mathematical operations and working with integers and floats. It demonstrates how to use Python as a calculator and perform arithmetic operations.

Key Takeaways
  1. Use Python as a calculator to perform simple mathematical operations
  2. Understand the difference between integers and floats
  3. Use the round() function to round numbers to a specified number of decimal places
  4. Understand the order of operations in Python
💡 Python has a priority on certain mathematical operations, and the order of operations can affect the result of an expression.

Related Reads

Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →