Python variables and data types you should know as a beginner ❎

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody in this topic I'm going to explain variables and data types in python as well as give you a few useful tips and tricks at the end of this topic so why don't you sit back relax and enjoy the show let's talk about variables a variable is a reusable container for storing a value a variable behaves as if it were the value it contains think back to algebra class back in the day we have an equation that contains x x is some representation of a value wherever we use x it behaves as if it were a certain number that's kind of the same thing with programming now when we create a variable we would like to give a descriptive and unique name of what that variable contains suppose I'm working with a user's Edge I could declare a variable named age age equals then some value let's say my age is 21 not anymore but I like to think it still is guys I'm getting old I can use this variable age and it will behave as if it were the number 21. to print a variable you can place it within a print statement print age my age is 21. if you're printing a variable you don't need to put it within quotes because what we're doing then is literally printing the word age and not the variable if you need to display a variable along with some text such as you are age years old what we could do is shrink concatenation wherever you would like to include a variable let's say here where ages we can separate this line of text into different strings you are plus age plus the text years old so we do have an error here type error can only concatenate string not into Strings we can't add numbers to Strings directly we would need to do what is called typecasting to display a number along with subtext we would need to cast this variable which is a number into text a string to do that we can precede the variable name with s t r then surround the variable with a set of parentheses if I were to run this again this will display you are 21 years old but you do have to pay attention to your spaces I'm going to add a space after r as well as before the word years run this again you are 21 years old another way to display a variable along with some text is to separate the text and variables in two separate arguments print then write our text within quotes you you are wherever you would like to add a variable add a comma then the variable name if you have more text that follows add another comma then add some additional text you are age years old so what we've done is separated our strings and variables into different arguments each separated with a comma but if you separate your variables and text into separate arguments you'll include a space automatically so I'm going to get rid of the space after R and before the word ears so spacing is pretty important depending on which method you use these two print statements will output the same thing you are 21 years old the third way to print a variable along with some text and this is becoming the more popular way of doing things is to use what is called an F string print a set of quotes precede your quotes with f u r wherever you would like to insert a variable add a set of curly braces the curly braces are acting as a placeholder for a value or variable we will place our variable age within the curly braces so yeah those are three different ways to Output a variable along with some text as of the filming of this video fstrings are becoming much more popular for output for the rest of the series we will be using fstrings but you should still be aware of the existence of these other two methods as well I need to discuss different data types there's four basic data types in Python there's still more but these are four for beginners we have integers floats strings and booleans let's begin with integers we'll create three integer variables for example we have age another whole number could be maybe players or users you're not going to have half a player right or nine tenths of a player it's going to be a whole number you have one two three or more players all whole numbers then maybe quantity maybe somebody is buying something quantity equals five you wouldn't have like half a product right it would be a whole number then let's display some of these variables print we'll use fstrings because we like fstrings you are age years old this is the same from the previous example let's use players there are players than the word players online then quantity let's use this you would like to buy our variable quantity items let's run this you are 21 years old there are two players online you would like to buy five items so those are integers they're just whole numbers I'm going to turn these lines into comments and then we can continue let's move on to floats a float is a number that contains a decimal portion for example maybe a GPA that's usually a decimal my GPA is 3.2 what about a distance this could be kilometers miles whatever 2.5 kilometers um price price could be afloat price equals 10.99 let's display some of these let's print our GPA your GPA is GPA let's print distance you ran distance then I'll add km for kilometers print the price is I'm going to add a dollar sign then our placeholder with price and that should be good your GPA is 3.2 you ran 2.5 kilometers the price is 10.99 so those are floats they're numbers that contain a decimal portion even if the decimal had point zero it would still be considered a float whereas this would be an integer so those are floats I'm going to turn these lines into comments then we can move on now we have strings a string is just a series of text for example maybe a username name equals type in your first name it's just a series of characters those are strings how about food what's your favorite food I like pizza I'll add that to my variable food then what about an email email equals make up some email bro123 gmail.com now with strings they can contain numbers but we treat them differently than integers and floats integers and floats we can use with arithmetic equations here they're more or less just characters so a string is just a series of characters within quotes these can be single quotes or double quotes then let's display these for practice let's say hello our variable name you like our variable food your email is our email variable hello bro you like pizza your email is bro123 gmail.com so those are strings it's a series of text okay then lastly we have booleans a Boolean is either true or false it's binary it's kind of like a light switch a Boolean variable can have only one of two states true or false typically booleans are used internally within a program for example maybe we have a Boolean variable named online if somebody is online this can be set to true if they're offline it could be false let's set that to true what if something is for sale four sale if it's available we could say true if not it's false it only has two states then what about running is some process running running equals true or false then let's print these print what could we say with online are you online then let's print our variable online print is the item for sale for sale perhaps running refers to a game is the game running game running colon space our running variable so are you online true is the item for sale false game running true typically with booleans you use these internally it's not common to print them directly usually we would use these with if statements such as if then the Boolean if some process is running print the game is running else print the game is over I'm just going to turn these into comments so if running is true the game is running if it's false you would do something else the game is over but I'll discuss this more when we reach if statements you don't need to know them at this point in time just understand for now a Boolean is either true or false oh here's another thing a common mistake that people do with booleans is that they may put them within quotes you can see that the color scheme changed this is technically a string it's within quotes so that's a common mistake that people might make if you're writing a Boolean make sure it's not within quotes and the first letter is capital before I wrap things up I'm going to show you a few tips and tricks with variables suppose we have three variables x equals one y equals two Z equals three then I'll print these variables directly print X print why print Z this would result in one two three another possibility in place of assigning these variables on separate lines you could do so all on one line we could write X comma y comma Z equals one comma two comma three that would do the same thing and it only takes one line of code this is referred to as multiple assignment if you ever need to set multiple variables to the same value this is what you could do I need to set X Y and Z all to zero maybe they are coordinates or something what I would instead type is x equals y equals z equals zero or some other value so x y and z are all zero if I were to change this number to one well they all equal one that's how to set multiple variables to the same value variable one equals variable two equals however many more variables you have all right everybody those are variables a variable is a reusable container for storing a value a variable behaves as if it were the value it contains we discussed four data types integers which are whole numbers floats which are numbers that contain a decimal portion strings which are a series of text like a name or an email and booleans which are either true or false they're binary so yeah that's an introduction to variables in python in the comments section why don't you post four variables an integer a float a string and a Boolean try and think of a unique example that I didn't cover already so yeah those are variables in Python
Info
Channel: Bro Code
Views: 174,485
Rating: undefined out of 5
Keywords: python, python tutorial, programming, coding, coding tutorial for beginners, programming tutorial for beginners, javascript tutorial for beginners, java tutorial for beginners, C++ tutorial for beginners, python variables
Id: LKFrQXaoSMQ
Channel Id: undefined
Length: 13min 30sec (810 seconds)
Published: Mon Oct 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.