A fundamental part of any programming language is the use of variables and constants—placeholders for your data.
A fundamental part of any programming language is the use of variables and constants—placeholders for your data. In this module, you’ll learn how to use the Swift built-in keywords of “let” for constants and “var” for variables, and you’ll learn how to assign those keywords to these placeholders for your information.
Variables and constants can hold different types of data, and in this module, you’ll begin your exploration of those types. We’ll start with the basic data types, including strings. You’ll learn how to manipulate strings and how to use concatenation to bring different strings together. You’ll also learn how to use string interpolation, which is Swift’s improved variation of concatenation. In addition, you’ll learn about the number data type, including the whole number data type called Int and the decimal number data type called Double. You’ll also gain deeper insight into the Boolean data type, which you can use to set a variable or constant to “true” or “false” and then evaluate that condition later in your logic.
Learning Objectives
- Describe variables and constants
- Explain strings, numbers, and Booleans
- Identify how to put strings together
Skills you’ll gain
Data BindingData Definition LanguageLocal VariablesSwift (Programming Language)Visual Programming Language (VPL)What You'll Learn
- Use the Swift keywords "let" for constants and "var" for variables to hold your data
- Describe variables and constants as placeholders for information
- Manipulate strings and combine them using concatenation and Swift's string interpolation
- Work with number data types, including the whole number type Int and the decimal type Double
- Explain the Boolean data type to set values to "true" or "false" and evaluate that condition in your logic
- Work with variables, constants, and tuples in Swift
Key Takeaways
- Variables and constants are placeholders for your data, and Swift uses the keyword "let" for constants and "var" for variables.
- Variables and constants can hold different types of data, including basic data types such as strings.
- String interpolation is Swift's improved variation of concatenation for bringing strings together.
- Swift's number data type includes the whole number type called Int and the decimal number type called Double.
- The Boolean data type can set a variable or constant to "true" or "false" so the condition can be evaluated later in your logic.
Frequently Asked Questions
What topics does this Swift module cover?
This module covers using variables and constants with the "let" and "var" keywords, basic data types including strings, string manipulation through concatenation and string interpolation, the number data types Int and Double, and the Boolean data type. Its lessons are Variables, Constants, and Tuples; Strings and String Interpolation; Numbers; and Booleans.
Who is this course for?
As indicated by the title "Swift Beginner: Data and Variables," this module is aimed at beginners starting their exploration of data types in the Swift programming language.
What skills will I gain from this course?
The course develops skills in Swift (Programming Language), Data Binding, Data Definition Language, Local Variables, and Visual Programming Language (VPL).
What is string interpolation in Swift?
String interpolation is Swift's improved variation of concatenation, which is used to bring different strings together.
What is the difference between "let" and "var" in Swift?
In Swift, "let" is the built-in keyword for constants and "var" is the keyword for variables; both are assigned to placeholders that hold your data.
Transcript
Show transcript (free preview lesson)
Transcript of the free preview lesson. Remaining lessons unlock with the full course.
Hello, my name is Mark Nair. And in these lessons, you'll learn about constants, variables, and tuples, strings and string of interpolation, numbers and Boolean values. Constants and variables are the basic data structure or a holder or container of information that we use in programming. So we manipulate data all day long, moving things back and forth, getting somebody's name, putting their name up on the screen, tests that's just a bit of data. So we need to use holders for that or containers. We call these constants and variables. Think about constants and variables much like a box that holds information. Some boxes are empty. They have nothing in it yet. Some boxes are full, but you can switch things out. There's a teddy bear in there and take it out and you put in an orange. Some boxes are taped shut. They contain a thing and that's in it. No matter what you can't take it out and replace it. So that's what we are going to look at now. Constant and variables. The way we do this is we set up, we'll look at constants first, we set up a constant with a keyword let. So this tell Swift we are going to establish a constant value. Then we name the constant case. I'll just say firstPlanet. This is going to be the name that I'll refer to for now on for this constant. I need to give that a value. So I will say equals Mercury. So here in my constant is called firstPlanet and that equals has a value of this thing in quotation marks called Mercury. This doesn't have to be something that is specific and never changing in the real world. I mean, I can say let playerName is equal to Sarah which just means when the code starts running, the player that we're dealing with right now is Sarah. Throughout the rest of the time, it's Sarah. Later on if we restart it again, that player name could be something else, but it is a constant value within the holder of player name, and that's Sarah. Now, previously we have used the print command and we've said something like this, print Sarah. And it printed to the debug area right here, this value of Sarah. But now that we've assigned this information or this information, we can do the same thing in the print area by just using the name of the constant right there. And as soon as I do it, there is Mercury. So now we don't really have to use the specific data, the hard coded information, we can start using the names of our constants or variables. So just keep in mind. Then when you want some information to stay static, to stay, we also call immutable, but stay alone without being changed for the duration of the lifetime of your program, use a constant. Now there are times though, that you want to change information. And for that, we'll use a variable. We define a variable similar to a constant with a keyword but this keyword is var, stands for variable. Then we name, the name that we'll use. So this case I'll say playerScore and on give that a value of 31. Now this playerScore can change throughout our program. So I can easily say playerScore is now equal to 40. And now if I take a look at playerScore in the preview area and print this, you see how it goes from 31 to 40, and now it is 40. And I can do the same thing, playerScore is now equal to 100 and you'll see the result. So it changes. I cannot though do this, firstPlanet. If I try to do this, I will get a lot of warnings and a crash because firstPlanet is a let constant but I'm treating it as a variable and I can't do that because I established it up here as a let. Okay, lemme get rid of that. Get rid of all that. We can also start taking information inside of our code and attributing it or using it with other bits of information. By this I mean, let me take a look at this. I'll say currentTemperature is equal to 42.3. That's a good variable because the temperature will change. CurrentLocation is equal to Happy. All right, so far so good. Because the current location is going to change depending on where we are, and that's kind of defined in our word here anyway. But if I want to have a saved location, what I can do is say var savedLocation. So I'll keep my camel case going. SavedLocation is equal to currentLocation. And now if I take a peek into savedLocation, that should show me Happy, Texas right here. So now you can see how we pass information back and forth. There are a couple of rules about constants and variables. They can't contain mathematical symbols. You can't start with a number in your naming. I can't say var the number three temperature, for example. And they can't contain spaces and that's why we use camel case for all of this. I can't say let first space planet, that won't do and you'll get tremendous warnings about that. So I had said earlier, we need to give it a value when we start it, when we actually create the constant or the variable. It's not necessarily true. I could say something like this, let lakeWaterLevel. Now, if I just leave it alone, I'll get an error. That error says, "type annotation missing in pattern." One of my favorite strangely worded errors. But what it's saying is, Hey I don't understand what you're saying. You have nothing else happening here. You didn't give it anything or you are not really completing the meaning. I don't know. Let's say I don't know the water level but I still need to establish this constant so I can give it a type instead. I can say this type will be a type Double. Now when I have that, the error goes away. Everything is okay. Unfortunately, if I try to do something with it so let me just try to print it, lakeWaterLevel. I'll get an error because there is nothing to print. it's called being initialized. I haven't given it any value. So what I can do here is say later on in my program I finally get a value for that. I do this, then it all works okay. So you do not necessarily at the beginning have to give variables and constants of value as long as you tell Swift it's a type and Swift can't, they can't understand what that type is based on the value 'cause there is no value. So Swift says, Hey man, I need a little bit of help please. One other thing. There's a thing called the tuple and a tuple is values inside of a variable or constant that you might have more sophistication in them. So let's take a look at that. Let bicycle equal, the name of bicycle let's say is Precious. All right, so far so good. We know that this value is inside of this container, this constant. But let's say I have more information, but I don't really wanna have a giant data structure for it. I can do this by putting this information in parentheses and let's say, I wanna have the brand. So Firefly, Firefly is a brand, and the derailer is a Shimano. and the PSI for the tires is 125. Okay, I put these in parentheses and now all of this information is inside of bicycle. And this structure right here is called the tuple. To access that information, I'll just put bicycle, and the popup I see zero, one, two, three. If I access one for example and run the code, I get Firefly. Because position zero, and you can see it here, in the preview is Precious. Position one is Firefly, Two is Shimano. This isn't exactly helpful, 'cause it's hard to remember what numbers mean so we can label these. I can put in name, brand just like this, derailer and air pressure. Now when I try to access those values, bicycle., these names pop up and I can use it just like that and there is Precious. So a quick way of putting information data into a variable or constant without going overboard with the data structure but it's really for temporary use, it's not really a sophisticated thing. I just wanted to show you that while we're talking about. All right. Thanks for watching. Stay tuned for the next lesson about strings and string interpolation.
Learn on the Go
Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.