# Variables

Introduction to variables and basic data types.

Start## Key Concepts

Review core concepts you need to learn to master this subject

Variables

Variables

`var score = 0`

A variable refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data.

Constants

Constants

`var score = 0`

Constants refer to fixed values that a program may not alter during its execution. One can be declared by using the `let`

keyword.

Arithmetic Operators

Arithmetic Operators

`var score = 0`

Swift supports arithmetic operators for:

`+`

addition`-`

subtraction`*`

multiplication`/`

division`%`

remainder

Types

Types

`var score = 0`

Type annotation can be used during declaration.

The basic data types are:

`Int`

: integer numbers`Double`

: floating-point numbers`String`

: a sequence of characters`Bool`

: truth values

String Interpolation

String Interpolation

`var score = 0`

String interpolation can be used to construct a `String`

from a mix of variables, constants, and others by including their values inside a string literal.

Compound Assignment Operators

Compound Assignment Operators

`var score = 0`

Compound assignment operators provide a shorthand method for updating the value of a variable:

`+=`

add and assign the sum`-=`

subtract and assign the difference`*=`

multiply and assign the product`/=`

divide and assign the quotient`%=`

divide and assign the remainder

- 1The “Hello, world!” program simply writes to the screen. It does not read anything, calculate anything, or allow for user input. That’s no fun! Real programs tend to produce results based on some …
- 2Suppose we are building a game, and we want to keep track of the player’s name, the player’s score that goes from 0 to 10, the player’s level, etc. We need some variables! Before we can use a var…
- 3Computers are incredible at doing calculations. Now that we have declared variables, let’s use them with
*arithmetic operators*to calculate things! Here are some arithmetic operators: - + additi… - 4Sometimes the value of our variables need to be adjusted. We know that we can update our variables using arithmetic operators like this: var age = 99 age = age + 1 print(age) // Prints 100 We a…
- 5We can provide a
*type annotation*when we declare a variable to be clear about the kind of value that the variable can store. Some of the basic data types: - Int: integer numbers - Double: float… - 6Often times, we find ourselves needing to insert values into a string. In these cases, we can use
*string interpolation*. In Swift, to insert a value into a string using string interpolation, we … - 7We’ve manipulated variables in the last few exercises, but what about variables with values we don’t want to change? That’s the job for
*constants*. Whereas variables can be reassigned during thei… - 8Now that we’ve learned about the basics of variables and arithmetic operators, let’s write a program! The mad scientist Kelvin has mastered predicting the weather in his mountain-side meteorology …
- 9You made it to the end of the lesson! High five. 🙌 Here is a review of the lesson: - A variable represents a particular piece of your computer’s memory that has been set aside for you to use to s…

## What you'll create

Portfolio projects that showcase your new skills

### Dog Years

Dogs have ages that don't quite work like ours. This can be a little ruff when you want to know how old your dog would be if he/she is a human. Don't fret — build a dog-age-converting program to translate your dog's age into human years!

### Quadratic Formula

Write a Swift program that solves the quadratic equation.

### Piggy Bank

You just returned from a trip to South America and you came back with three different kinds of currencies. Let's convert them to USD!

## How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory