# 3 - Variables and Basic Math ## Are you teaching me Algebra now? No not really. In Algebra, you might be used to single letter variables, maybe some special characters if you've taken High School Physics already, and you're often trying to solve for one of these hidden variable values, or maybe you have a mix of known and unknown values. Python (and other programming languages) use variables to store information we want to reference in our program at a later time, the variables you'll see in programming are more dynamic that what you might have to deal with in Algebra. ## What can variables store? This is kind of a loaded question, the answer is a lot of things. For now, for this lesson, we're going to constrain ourselves to just talking about 4 types of variables, they are: - Strings - This is text, usually represented by surrounding the text in double quotes - ints (Integers) - These are whole numbers, both negative and positive and 0. You CAN'T have a decimal in an int value - Booleans (True/False) - These are on/off, true/false values, you'll see booleans used when your code needs to make choices about what to do next - Floats (Decimals) - These are real numbers, both negative and positive and 0. While there are limitations to what you can have for numbers here, decimal numbers are allowed and exceptionally small fractions are allowed, much smaller than what you'll need to deal with Robot Programming ## How do I create a variable? In the early examples, creating a variable is as easy as the following \ = \ Example: fruit = "Apple" Example: teamNumber = 2648 Example: zuchinniNasty = True Example: radius = 1.423563 When you want to use a variable in your program, simply type its name print(fruit) print("Team Number: " + str(teamNumber)) ## How do I know if a variable is a particular type? For the most part, you don't. Python does it's best to automatically assign variables the appropriate type in the background, in the hopes that you won't have to think about it. A lot of other programming languages don't do this, as it can cause problems when trying to do certain things. If you are working on a part of your program and you want a value to be a certain way, most of the 4 types we talked about (with the exception of boolean) have a easy way of trying to convert on type to another str(value) - Will try to convert value to a string int(value) - Will try to convert value to an integer float(value) - Will try to convert value to a decimal Note that, if you try to convert something ridiculous like int("Banana"), you're going to get an error, because it really doesn't make sense to take some text, and turn it into a number. ## Is math any different than what I'm used to? For the most part no, you have addition, subtraction, multiplication, and division. You also have another operation, modulous, or remainder, this does a division operation, but rather than return the number of times the second number goes into the first, it returns the amount left over that couldn't be accounted for. (Assume we're working with only integers here) Example: 5 / 3 = 1, 5 % 3 = 2 Example 8 / 4 = 2, 8 % 4 = 0 Remainder only works on integers, floats should (but may not always) return an error. ## What about power, cosine, sine, tangent, etc? Those do exist! But they're a bit further into math than we need to get right now. If you want to know more, Google "Python math module". Basically, all of those things exist, but as an extra "component" you need to "import" into your code in order for them to become avaialble to use. There will be more information about importing modules later, as it becomes more important the more complicated the programs you want to create become.