The Joy of Programming
Hello, Python!

In this lesson, we are going to get glimpse of Python programming language.

Python is a very simple language to learn. While it has many features, we just need a small subset to get started.

Printing Values

Let's write a program to print a number.

print(4)

The print is a function that takes a value and shows it in the output area.

We can also use the print function to print multiple values at once.

print(1, 2, 3)

In Python, text values are enclosed in quotes and they are called strings.

Let's see a simple example using strings.

print('Hello, Python!')

In the above example, we are passing a string value to the print function and it displays that value.

We use string to represent names of colors etc. in this course. You'll see more of this in the upcoming lessons.

Comments

While we write programs for the computer to perform some action, it is also equallly important to pay attention to make the program easy to understand for other people or even yourself.

Every programming language provides a way to write comments in the code. Comments are notes that we write in the program, which are ignored by the computer, but they help people to understand what the program does or why the program was writen in that particular way.

In Python, comments start with # character. Everything from # to the end of the line is considered as comment and is ignored by the computer.

The following example, the first two lines are comments.

# Program to print Hello, world!
# Click on the 'Run' button to execute this program

print('Hello, Python!')

Working with Numbers

Python supports usual arthemetic operations +, -, * and / for addition, subtraction, multiplication and division respectively. Please note the the multiplcaition operator is *, called an asterisk or a star. There is also an operator % to find the remainder.

Let us try doing some simple computations using these operators.

print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
print(7 % 2)

As you can see the result of 7/2 is decimal number 3.5. Python know how to handle various types of data, but for now we will just stick to numbers, both integers and decimal numbers.

Big Numbers

Python is pretty good at handling big numbers. It is a lot of fun to play with really big numbers.

Can you guess what would the output of the following computation?

print(111111111 * 111111111)

Python also has an exponential operator **. If you want to compute the value of 210, you need to write it as 2 ** 10 in Python.

print(2 ** 10)

Why stop at 210? How about 2100?

print(2 ** 100)

Is that a big number? Not really! Try to compute 21000 and see the result.

print(2 ** 1000)

Isn't that interesting? Why stop at 21000? Do you want to try with 10000 or even 100000?

Computers are really good at doing lot of computations. We just need to learn how to tell them what to do.

Exercise 1.2: Factorial of 5

Write a program to calculate the factorial of 5. It is computed by multiplying all the numbers from 1 to 5.


Variables

Sometimes, it is handy to give a name to a value and refer it by the name. In Python, it is done using variables.

x = 5
y = 2
print(x+y)

In the above program, x and y are variables holding values 5 and 2 respectively.

Variables can hold any type of values. In the example below, the variable name has a string value.

name = 'Joy!'
print(name)

Notice the different between using a variable and a string by looking at the output of the following program.

name = 'Joy'

print('name')
print(name)

The first print statement is printing a literal string name while the second one is printing the value of the variable name, which happened to be Joy.

Lists

Sometimes, we need to deal with a list of values. In python lists are written using the following syntax.

numbers = [1, 2, 3, 4, 5]
print(numbers)

We could even have a list of names.

names = ['Alice', 'Bob', 'Charlie']
print(names)

You may be wondering why the output has single quotes around the names while we have given double quotes. Python doesn't distinguish between double quotes or single quotes. The output is going to be the same irrespective of the kind of quotes you've used.

We could also create a list by using the values of existing variables.

a = 'Alice'
b = 'Bob'
c = 'Charlie'
names = [a, b, c]
print(names)

There are some functions that operate on lists. For example, the len function computes the length of a list and the sum function computes the sum of all numbers in a list.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(len(numbers))
print(sum(numbers))

Congratulations! Now you know enough Python to get started with drawing interesting shapes and patterns. Move on!

Questions
GG Gopika G
3 years ago
Post
Dismiss
I've submitted the excersise. But the progress is not showing in the home page. Y it so I've completed exercises but 0% is showing..
I've submitted the excersise. But the progress is not showing in the home page. Y it so I've completed exercises but 0% is showing..
JJ Joseph Jomy
3 years ago
Post
Dismiss
Hey! So I think the problem is after completing a certian section, you have to click on "Mark as Complete" which is located between the "Next" and "Previous" buttons. Then the progress bar will automatically update. Happy Learning!
Hey! So I think the problem is after completing a certian section, you have to click on "Mark as Complete" which is located between the "Next" and "Previous" buttons. Then the progress bar will automatically update. Happy Learning!
Want to discuss?
Post it here, our mentors will help you out.
can we bring word and calculations on single line
AS Aditya Suresh
3 years ago
Post
Dismiss
Can we print a string and a computation together on a single line
Can we print a string and a computation together on a single line
M mariyam
3 years ago
Post
Dismiss
no
no
AP Aysha PS
3 years ago
Post
Dismiss
nnop
nnop
TEEMRU
2 years ago
Post
Dismiss
print((7+5),"a","b") result = 12 a b
print((7+5),"a","b") result = 12 a b
Want to discuss?
Post it here, our mentors will help you out.
How to join joy of progamming
AA Ashana Ahad
3 years ago
Post
Dismiss
I find it dufficult to join for the course. Kindly help me out
I find it dufficult to join for the course. Kindly help me out
Want to discuss?
Post it here, our mentors will help you out.