Intro to Programming Tutorial menu

Variables

A variable is a named box in memory where your program keeps a value, like a name, an age or a price.

Creating a Variable

Imagine a label on a jar in a kitchen: the label is the variable's name, and what is inside is its value.

In Python you just write the name, an equals sign =, and the value:

Example

python
student = "Ali"
age = 17
print(student)
print(age)
Output
Ali
17

In JavaScript you start with the word let:

Example

javascript
let student = "Ali";
let age = 17;
console.log(student);
console.log(age);
Output
Ali
17

The = sign does not mean "equals" like in maths. It means "put this value into this box".

Changing a Variable

The value in a variable can change. That is why it is called a *variable*.

Example

python
price = 300
print(price)
price = 350
print(price)
Output
300
350

Example

javascript
let price = 300;
console.log(price);
price = 350;
console.log(price);
Output
300
350

In JavaScript you use let only the first time. After that, you just use the name.

In JavaScript, use const instead of let for a value that should never change, for example const shopName = "Karachi Mart";. Trying to change a const gives an error.

Types of Values

Values come in different kinds, called data types. The most common ones are:

TypePython exampleJavaScript exampleMeaning
Text (string)"Ayesha""Ayesha"Letters, words, sentences
Whole number2525Numbers without a decimal point
Decimal number99.599.5Numbers with a decimal point
True / FalseTrue, Falsetrue, falseA yes-or-no value (boolean)

Notice that Python writes True with a capital T, while JavaScript writes true in lowercase.

Using Variables Together

Variables become useful when you combine them. Here a small shop in Karachi works out a bill:

Example

python
item = "Notebook"
price = 120
quantity = 3
total = price * quantity
print(item, "total: Rs", total)
Output
Notebook total: Rs 360

Example

javascript
let item = "Notebook";
let price = 120;
let quantity = 3;
let total = price * quantity;
console.log(item, "total: Rs", total);
Output
Notebook total: Rs 360

Both print() and console.log() can show several values at once if you separate them with commas. They put a space between each value.

Joining Text

You can join strings with +. In Python, a number must be turned into text first with str():

Example

python
name = "Ayesha"
marks = 88
print("Student: " + name + ", marks: " + str(marks))
Output
Student: Ayesha, marks: 88

Example

javascript
let name = "Ayesha";
let marks = 88;
console.log("Student: " + name + ", marks: " + marks);
Output
Student: Ayesha, marks: 88

JavaScript turns the number into text for you automatically.

Naming Rules

Good names make code easy to read. Follow these rules in both languages:

  • Names can use letters, digits and the underscore _
  • A name cannot start with a digit (2price is not allowed)
  • No spaces: write total_price (Python style) or totalPrice (JavaScript style)
  • Names are case sensitive: Age and age are two different variables
  • Choose names that describe the value: fee is better than x

Exercise

Exercise

After these lines run, what is the value of score? score = 10, then score = score + 5

Learn Intro to Programming with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp