Code / Python for Beginners / Unit 1: Hello, Python

Variables and types वेरिएबल और टाइप

Lesson 2 of 14 · about 12 minutes · Store values in names; int, float, str and bool.

⚡ 0 XP

Step 1 of 2

Labelled boxes

"Pune"city

A variable is a name that holds a value, like a labelled box. Create one with =:

  • player = "Smriti" puts the text Smriti in a box called player
  • runs = 48 puts the number 48 in a box called runs

The = means store, not "equals". So runs = runs + 4 means: take the old runs, add 4, store the answer back in runs. Now runs is 52.

Name rules: use small letters and underscores (total_runs), no spaces, and don’t start with a number.

Example
player = "Smriti"
runs = 48
print(player, runs)
runs = runs + 6   # a six!
print(player, runs)

🎯 Challenge: She hits a four! +10 XP

There are two variables: player and runs. The player hits a four: add 4 to runs (write runs = runs + 4). Then print the player and her runs with one print(player, runs). The output should be Smriti 52.

Try twice to unlock the solution (0/2)

main.pyCtrl ⏎
Output

What your program prints appears here.

Checklist

Press Run to check your code.

  • runs is now 52
  • Adds 4 to the old value of runs
  • Prints Smriti 52
Lesson notes (text version)

1. Labelled boxes

A variable is a name that holds a value, like a labelled box. Create one with =:

  • player = "Smriti" puts the text Smriti in a box called player
  • runs = 48 puts the number 48 in a box called runs

The = means store, not "equals". So runs = runs + 4 means: take the old runs, add 4, store the answer back in runs. Now runs is 52.

Name rules: use small letters and underscores (total_runs), no spaces, and don’t start with a number.

                player = "Smriti"
runs = 48
print(player, runs)
runs = runs + 6   # a six!
print(player, runs)
              

Challenge: She hits a four!

There are two variables: player and runs. The player hits a four: add 4 to runs (write runs = runs + 4). Then print the player and her runs with one print(player, runs). The output should be Smriti 52.

2. Every value has a type

Python values come in types. The four you will use most:

  • int: whole numbers like 15 or -3
  • float: decimal numbers like 1.62 or 99.5
  • str (string): text in quotes like "Jaipur" or "15"
  • bool: only True or False (capital T and F!)

type(x) tells you the type. Watch out: 15 is an int but "15" is a str, because of the quotes. "15" + "1" gives "151" (text glued together), while 15 + 1 gives 16.

                print(type(15))
print(type(1.62))
print(type("15"))
print(type(True))
print("15" + "1", 15 + 1)
              

Challenge: Fix the student form

Someone typed every value of this form as text. Fix it so that:

  • age is the int 15
  • height is the float 1.62
  • city stays the str "Jaipur"
  • is_student is the bool True

Then print type(age) to check.

Search by task, format or tool name.