Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data

Corey Schafer · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

This video tutorial covers the basics of working with numeric data in Python, including integers and floats, arithmetic operations, and comparisons, using tools like Python 3 and built-in functions such as abs and round.

Full Transcript

hey there how's it going everybody in this video we'll be learning how to work with numeric data in Python and numbers are most commonly represented with integers and floats and the difference between an integer and a float is that an integer is a whole number and a float as a decimal so to see an example of this let's create a variable called num and let's just set this equal to 3 now python has a built-in function called type where we can see the data type of an object so if we print out the type of num and run this and we can see that it returns that that is of the class integer now if we were to set this number instead to 3.14 and now rerun this and we can see that now the type of number is a float so that's the main difference between an integer and a float now when working with numeric data it's common that you'll need to use some basic arithmetic so let me grab some comments from my snippets here just so that we have a reference for everything that we can do so I'll copy these arithmetic operators and paste these in and now let's go ahead and just run through each of these so the first four we've likely already seen a lot and are familiar with so for example addition we if we print it out 3 plus 2 and we can expect that equal to 5 if we print out 3 minus 2 that should be 1/3 times 2 should be equal to 6 and 3 divided by 2 should be equal to 1.5 now for division to behave this way this is actually new in Python 3 if you're running Python 2 then this will actually equal 1 because it drops the decimal but in Python 3 that gives us the right answer of 1.5 now if we don't want to drop that decimal then we do have a floor division and for division can be performed by adding 2 division signs so if I run this and now we can see it drops that decimal and it's equal to 1 so if you ever see these 2 division signs then that is this floor division now if you wanted to work with exponents and powers and we can use these 2 multiplication signs so if we wanted to print 3 to the second power then we could just say 3 with these two multiplication signs if I print that then we can see that that equals nine because three squared is equal to nine now this last operator here is called a modulo operator and it gives us the remainder after a division so three mod two will have a remainder of 1 because 2 goes into 3 once with one left over so if we say 3 mod 2 and run that we can see that that is equal to 1 now a common use case for this is to tell if a number is even or odd now the reason for this is because every time you divide a number by 2 there are only two possible remainders it's either going to be 0 or 1 so for example if we look at a few more examples here so let me just print out a few more module operators and I'll do 2 mod 2 3 mod 2 4 mod 2 and 5 mod 2 so if we run this then we can see that 2 goes into 2 once with no remainders that's why we get a 0 2 goes into 3 once with 1 as a remainder so 2 goes into 4 twice with no remainder and 2 goes into 5 twice with 1 as a remainder so we can see from this pattern that if you do a mod 2 on any number and there is no remainder and that number is even if you do a mod 2 on any number and the remainder is 1 and that number is odd and that's a pretty common check that you'll use a lot throughout Python programming ok so now let's look at the order of operations just like we would expect we can also use parentheses to change the order of operations just like with normal arithmetic so for example if we were to say let's see 3 times 2 plus 1 and with the normal order of operations we would multiply 3 & 2 which would give us 6 and then we would add 1 which would give us 7 so if we run that then we can see that we got 7 as our answer but if I put a parentheses here around this 2 plus 1 and now with normal arithmetic the way that this would work is that it should first add up these numbers in the parentheses which should give us 3 and then 3 times 3 should give 9 so now if we run this then we can see that we got 9 so the order of operations does work correctly within Python like we would expect ok so now let's look at another common operation that you'll see a lot and that is incrementing a variable so if I make a variable here called num and I set this equal to 1 then what are some ways that we can increment that value by one well one way that we could do this is to say that num is equal to num plus 1 and if we print out that num that we can see that it did increment it by 1 and now it's equal to 2 but incrementing values is such a common operation that there is a shorthand for this so instead we can just say num plus equals 1 so if we run that that we can see that it's still incremented at value up to 2 and you can use this syntax with the other operations as well so instead if we were to say num x equals 10 and ran this then we can see that we got 10 because it took our original number and multiplied 1 by 10 okay so a couple more things here we also have some built-in functions available to us to work with numbers and one of these is abs for absolute value and basically this will just remove the sign for many negative numbers so if I took the absolute value of negative 3 and I'll just clean up a couple lines there ok so if we were to print out the absolute value of negative 3 and run that and we can see down here that we just got the absolute value which is 3 now another built-in function that we have is round and by default this will round our values to the nearest integer value so if we said print the round of 3 point 7 5 and run that and we can see that 3 point 7 5 rounded up to 4 and we can also pass a second argument into our round function that tells it how many digits that we want to round to so if I put in a comma here and pass in a 1 as a second argument and now run this and what we're saying is that we want to round to the first digit after the decimal we can see that that rounded 23.8 okay so another common thing that you need to do when working with numbers is to use comparisons now we want to know if two values are equal greater than less than and all of that so to test this we can use comparison operators and I have some comments over here in my snippets with the comparison operators as well and I'm just going to paste over the arithmetic operators that we've already gone over and paste those in now these comparisons are going to return boolean switch our true/false values we'll be learning more about boolean in a future video when we go over conditionals but we'll see them here for the first time so let's say I have two variables here and we'll just call these two variables num1 and set that equal to three and we'll do num2 is equal to two so now let's run through all of these comparisons so first let's say that we wanted to check if these two variables were equal so I could say num1 and you can see up here that the equals comparison is double equals now you don't want to use the single equals because the single equals is this assignment here so the double equals is comparison a single equals as assignment so we want to compare his number 1 equal to num2 and if we run that and we can see that it returns false because those two values are not equal now if I wanted to check if they weren't equal then I could use the exclamation point before the equal sign here exclamation point equals and if we run that that we can see that we got true because these two values are not equal now I can check if num 1 is greater than num2 by using the greater than sign so and save that and run it and we can see that we got true because num1 is 3 number 2 is 2 so 3 is greater than 2 and if you wanted to check less than then you can just use the less than sign so if we run that you can see we got false and you can also use the equal signs with these as well so if I want to check if this was greater than or equal to and we could run that we can see that it's true and if we want to check less than or equal to then we can use those as well and if we print that we can see that we got false ok so now we're going to look at one more thing I'm just going to delete these in order to get some more room now I will have these comments up on my github page if you want to reference to the arithmetic operators and the comparisons that we just looked at okay so in the last video we learned about strings now it's possible that you have something that looks like a number but it could actually be a string so let's look at a problem that we can run into if that's the case and then we'll see how to solve it so let's say that you have some variables that look like numbers so maybe we read these in from a text file or got them from a website or something like that so just to give an example let's recreate our number one but this time we're going to set these equal and single quotes here we'll set this to 100 and num2 we'll set equal to inside single quotes 200 so I explicitly set these equal to strings so it's obvious to us that they're strings but it might not be so obvious to us if we got these values from somewhere else so now let's say that we want to add these values together so if I was to say print num1 plus num2 and if we run this then we can see that this isn't the result that we thought it would be now if you remember from our string video when we add strings together it just concatenates those together so this is what we would expect with strings but with numbers we would expect this to be 300 so in order to turn these into integers we're going to have to do something called casting and casting is super easy and Python so to cast these values from string to integers then we'll just add a couple lines here and I will copy these and we'll just say that num1 equals int num1 so we just casted that to an integer and we can do the same thing here with num2 so now if we save that and run it and we can see that we got three hundred so if you have an integer that's actually a string and you want to cast that to an integer then you can wrap that string in this int function or this int class here to create an integer okay so I think that is going to do it for this video I hope that now you feel comfortable working with integers and floating point values and in the next video we'll be learning about lists sets and tuples which basically allow us to hold sequences of data and is extremely useful to learn how to use properly but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those if you enjoy these tutorials and would like to support them there are several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you

Original Description

In this Python Beginner Tutorial, we will begin learning about integers and floats. Integers and Floats allow us to work with numeric data in Python. We will be learning how to perform basic arithmetic, as well as how to compare numeric values. Let's get started. The code from this video can be found at: https://github.com/CoreyMSchafer/code_snippets/tree/master/Python-Ints Watch the full Python Beginner Series here: https://www.youtube.com/playlist?list=PL-osiE80TeTskrapNbzXhwoFUiLCjGgY7 ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Corey Schafer · Corey Schafer · 0 of 60

← Previous Next →
1 Web fonts using CSS Font Face
Web fonts using CSS Font Face
Corey Schafer
2 Using Font Awesome in Desktop Applications (OS X)
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
3 Sublime Text 2: Setup, Package Control, and Settings
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
4 ArcGIS API for JavaScript Part 1: Our First Web Map
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
5 Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
6 Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
7 ArcGIS API for JavaScript Part 2: Starting Templates
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
8 Paver Patio Time Lapse
Paver Patio Time Lapse
Corey Schafer
9 Mac Tip: Ways to perform Screen Capturing and Screenshots
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
10 WordPress Plugins: Imsanity
WordPress Plugins: Imsanity
Corey Schafer
11 WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
12 Sublime Text 3: Setup, Package Control, and Settings
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
13 Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
14 Mac Tip: Adding Folder Stacks to the Dock
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
15 CSS Tips and Tricks: Add External URLs to Print Stylesheets
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
16 JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
17 JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
18 JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
19 JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
20 JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
21 JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
22 JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
23 Python Tutorial: if __name__ == '__main__'
Python Tutorial: if __name__ == '__main__'
Corey Schafer
24 Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
25 How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
26 Easily Resize Multiple Images Using Picasa
Easily Resize Multiple Images Using Picasa
Corey Schafer
27 Easily Resize Multiple Images Using the Mac Terminal
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
28 Python Tutorial: virtualenv and why you should use virtual environments
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
29 Python Tutorial: pip - An in-depth look at the package management system
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
30 Git Tutorial: Using the Stash Command
Git Tutorial: Using the Stash Command
Corey Schafer
31 How Software Engineers, Developers, and Designers can volunteer their skills
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
32 Git Tutorial: Diff and Merge Tools
Git Tutorial: Diff and Merge Tools
Corey Schafer
33 Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
34 Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
35 Python Tutorial: str() vs repr()
Python Tutorial: str() vs repr()
Corey Schafer
36 Programming Terms: DRY (Don't Repeat Yourself)
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
37 Programming Terms: String Interpolation
Programming Terms: String Interpolation
Corey Schafer
38 Programming Terms: Idempotence
Programming Terms: Idempotence
Corey Schafer
39 Python Tutorial: Namedtuple - When and why should you use namedtuples?
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
40 Programming Terms: Mutable vs Immutable
Programming Terms: Mutable vs Immutable
Corey Schafer
41 Python Tutorial: Else Clauses on Loops
Python Tutorial: Else Clauses on Loops
Corey Schafer
42 Overview of Online Learning Resources
Overview of Online Learning Resources
Corey Schafer
43 Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
44 Git Tutorial for Beginners: Command-Line Fundamentals
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
45 Quickest and Easiest Way to Run a Local Web-Server
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
46 Python Tutorial: Generators - How to use them and the benefits you receive
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
47 Python Tutorial: Comprehensions - How they work and why you should be using them
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
48 Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
49 Programming Terms: Combinations and Permutations
Programming Terms: Combinations and Permutations
Corey Schafer
50 Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
51 Preparing for a Python Interview: 10 Things You Should Know
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
52 SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
53 SQL Tutorial for Beginners 2: Creating Your First Table
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
54 SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
55 Linux/Mac Terminal Tutorial: Navigating your Filesystem
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
56 Python: Ex Machina Easter Egg - Hidden Message within the Code
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
57 Mac Tip: New Split Screen Feature in El Capitan
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
58 Setting up a Python Development Environment in Eclipse
Setting up a Python Development Environment in Eclipse
Corey Schafer
59 Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
60 SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer

This video tutorial teaches the basics of working with numeric data in Python, covering integers, floats, arithmetic operations, and comparisons, with practical examples and code snippets. By watching this tutorial, viewers can learn how to work with numeric data in Python and perform basic arithmetic operations. The tutorial is designed for beginners and provides a comprehensive introduction to numeric data in Python.

Key Takeaways
  1. Create a variable and assign it a value
  2. Use the type function to determine the data type of an object
  3. Perform arithmetic operations using addition, subtraction, multiplication, division, and modulo operators
  4. Use parentheses to change the order of operations
  5. Increment a variable using num += 1
  6. Use built-in functions such as abs and round
  7. Compare numeric values using comparison operators
  8. Cast a string to an integer using the int function or int class
💡 The order of operations in Python follows the standard mathematical rules, and parentheses can be used to change the order of operations. Additionally, Python provides built-in functions such as abs and round to perform common arithmetic operations.

Related Reads

Up next
Stop Losing Gym Members! AI Answers Every Call & Books More Tours | Gym Jarvis
Scale Through Automation
Watch →