EthicsComputerCourses
Join training +91 90532 10052

Your First Program

Intro to Programming · Lesson 7 of 25 Beginner 7 min read Updated

You have already run a few lines of code. Now you will write a complete small program of your own and understand every character in it: what the brackets do, why text needs quotes, how to leave notes for yourself, and how the computer works through the lines.

By the end of this lesson you will be able to print a neat canteen bill in both Python and JavaScript.

Showing text on the screen

The simplest useful program shows a message. In Python you use print(); in JavaScript you use console.log(). What you want to show goes inside the round brackets.

PythonRun it yourself
print("Good morning, Pune!")
print('Today is a holiday.')
Output
Good morning, Pune!
Today is a holiday.

A piece of text in a program is called a string (short for "a string of characters"). Strings go inside quotes. In both Python and JavaScript you can use double quotes "..." or single quotes '...'; just start and end with the same kind.

Each print() starts a new line. That is why the two messages appear on separate lines.

Showing numbers and sums

Numbers do not need quotes. If you write a sum without quotes, the computer works it out first and then shows the answer:

JavaScriptRun it yourself
console.log(4 * 15);
console.log(1299 + 499);
console.log("4 * 15");
Output
60
1798
4 * 15

In code, * means multiply and / means divide, because the keyboard has no × or ÷ keys. The last line has quotes, so it is a string and is shown exactly as written.

You can show several values in one line by separating them with commas. Both languages put a space between the values:

PythonRun it yourself
print("Samosas:", 4 * 15, "rupees")
Output
Samosas: 60 rupees

Comments: notes for humans

A comment is a note in your code that the computer ignores. Use comments to explain why you wrote something, or to switch off a line while you test. Python comments start with #; JavaScript comments start with //.

Python
# This line is a comment, Python skips it
print("Hello")  # A comment can also go at the end of a line
# print("This line is switched off")
JavaScript
// This line is a comment, JavaScript skips it
console.log("Hello"); // A comment can also go at the end of a line
// console.log("This line is switched off");

Both programs print only Hello. Good comments explain the reason behind the code, such as # shop closes at 9 pm, so stop taking orders after 8:45. Avoid comments that repeat the obvious, like # print hello.

Statements run from top to bottom

Each instruction in a program is called a statement. Normally you write one statement per line, and the computer runs them in order from the top. In JavaScript, a statement usually ends with a semicolon ;. Python does not use semicolons; the end of the line ends the statement.

Here is a complete program that prints a canteen bill. It has comments, text, numbers and sums:

PythonRun it yourself
# Canteen bill for Rohan
print("--- College Canteen, Rohtak ---")
print("Samosa x 4 =", 4 * 15)
print("Chai x 2 =", 2 * 12)
print("Total to pay:", 4 * 15 + 2 * 12)
print("Thank you, visit again!")
Output
--- College Canteen, Rohtak ---
Samosa x 4 = 60
Chai x 2 = 24
Total to pay: 84
Thank you, visit again!

The same program in JavaScript:

JavaScriptRun it yourself
// Canteen bill for Rohan
console.log("--- College Canteen, Rohtak ---");
console.log("Samosa x 4 =", 4 * 15);
console.log("Chai x 2 =", 2 * 12);
console.log("Total to pay:", 4 * 15 + 2 * 12);
console.log("Thank you, visit again!");
Output
--- College Canteen, Rohtak ---
Samosa x 4 = 60
Chai x 2 = 24
Total to pay: 84
Thank you, visit again!

Notice that the prices 15 and 12 are typed several times. If the price of a samosa changes, you must change it in two places and might miss one. The next lesson, Variables, fixes exactly this problem.

Quotes inside text and new lines

What if your text itself contains quotes? Use the other kind of quote around it. To start a new line inside a string, write \n, a special code that means "new line". The backslash \ starts an escape sequence: a short code for a character that is hard to type.

PythonRun it yourself
print('Kabir shouted "Six!" from the stands.')
print("Line one\nLine two")
print("Price: ₹250")
Output
Kabir shouted "Six!" from the stands.
Line one
Line two
Price: ₹250

₹ is the code for the rupee sign. You can also type ₹ directly, because both Python and JavaScript understand all Unicode characters, including Hindi and Tamil letters.

Common mistakes

  • A missing quote or bracket. print("Hello) or print("Hello" gives a SyntaxError. Every opening quote and bracket needs a matching closing one.
  • A capital letter. Code is case-sensitive. Print("Hi") fails with NameError: name 'Print' is not defined. Did you mean: 'print'? In JavaScript, Console.log fails the same way.
  • Curly quotes. Text copied from WhatsApp or a word processor often has curly quotes like “Hello”. Python reports SyntaxError: invalid character '“' (U+201C). Always type straight quotes in your editor.
  • Mixing the languages. print() is Python and console.log() is JavaScript. Using one in the other language gives an error (in a browser, print() even opens the print dialog).

Key points

  • print() in Python and console.log() in JavaScript show values on the screen.
  • Text values are strings and go inside matching single or double quotes.
  • Numbers and sums need no quotes; * multiplies and / divides.
  • Commas let you show several values on one line, separated by spaces.
  • Comments start with # in Python and // in JavaScript; the computer ignores them.
  • Statements run from top to bottom; JavaScript statements usually end with ;.

Exercise

Write a program that prints a bill for a stationery shop in Lucknow. Meera buys 3 notebooks at ₹45 each and 2 pens at ₹10 each. Print a heading line, one line per item with its cost, a total line, and a goodbye message. Add one comment that explains what the program is for. Write it in Python, then in JavaScript.

Show solution
Python
# Bill for Meera at the stationery shop
print("=== Lucknow Stationers ===")
print("Notebook x 3 =", 3 * 45)
print("Pen x 2 =", 2 * 10)
print("Total:", 3 * 45 + 2 * 10)
print("Have a good day!")
Output
=== Lucknow Stationers ===
Notebook x 3 = 135
Pen x 2 = 20
Total: 155
Have a good day!
JavaScript
// Bill for Meera at the stationery shop
console.log("=== Lucknow Stationers ===");
console.log("Notebook x 3 =", 3 * 45);
console.log("Pen x 2 =", 2 * 10);
console.log("Total:", 3 * 45 + 2 * 10);
console.log("Have a good day!");

The sums have no quotes, so they are calculated. The labels are strings inside quotes, and commas join each label with its number.

Quick Quiz

1. What is a string?

2. Which line starts a comment in JavaScript?

3. What does print("Total:", 2 * 50) show in Python?

4. Why does Print("Hi") fail in Python?

Learn Intro to Programming with a mentor

Free lessons get you started. Our Python Programming training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call