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.
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.
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!')
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.
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 2
10
, you need to write it as 2 ** 10
in Python.
print(2 ** 10)
Why stop at 2
10
? How about 2
100
?
print(2 ** 100)
Is that a big number? Not really! Try to compute 2
1000
and
see the result.
print(2 ** 1000)
Isn't that interesting? Why stop at 2
1000
? 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.
Write a program to calculate the factorial of 5. It is computed by
multiplying all the numbers from 1
to 5
.
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
.
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!