Lesson-1.1Python shell | Replit ConsolePromptsOutputEmojisLiterals and VariablesBasic Data Types | type()IntegerFloatStringBooleanComments
In this lesson, we will be working with the Python interpreter in the interactive mode. This is often called a Python shell. It is a tool that lets us execute individual lines of code and see the output right away. We will drop the phrase "interactive mode" and just refer to it as the interpreter. Have a look at the official Python documentation for more details about the Python Interpreter. If you have Python installed on your system, then the Python shell will look like this:
In Replit, this corresponds to the console screen on the right of the repl. This will be our playground for quite sometime:
The orange symbol that is displayed above is called a prompt. Its role is similar to that of the blinking cursor while editing documents. It is an invitation to type code. Code that is typed at the prompt is executed by the interpreter. In these lessons, we will use the following symbol to refer to the prompt: >>>
.
We are all set to write our first line of code:
xxxxxxxxxx
21>>> print('Hello World!')
2Hello World!
Fire up a repl and type the code in the console. You should be getting the output on the next line.
Let us take a closer look at the first line of code that we wrote. print
is called a built-in function in Python. A function is an object that accepts inputs and returns outputs. The term built-in refers to the fact that this function is something that is readily provided by Python for our use.
xxxxxxxxxx
41>>> print('Hello World!')
2Hello World!
3>>> print("Hello World!")
4Hello World!
The object inside the parenthesis of the print
function is called a string. A string is a sequence of characters enclosed in quotes. Strings can either be in single quotes or double quotes. However, a single quote can't be matched against a double quote to enclose a string. We have used single quotes in line 1 and double quotes in line 3. Both lines give identical outputs. The ability to use both single quotes and double quotes comes in handy in situations like this:
Print a string that has an apostrophe in it:
xxxxxxxxxx
11>>> print("India's capital is New Delhi.")
Run the code given above and observe the output. print
can also be used to print numbers:
xxxxxxxxxx
41>>> print(1)
21
3>>> print(2.0)
42.0
Multiple items can be printed on the same line in the following way:
xxxxxxxxxx
41>>> print(1, 2)
21 2
3>>> print('online', 'degree', 'program')
4online degree program
Note the presence of a space between successive elements. If the print
command is called without passing any input to it, then it prints a blank line:
xxxxxxxxxx
31>>> print()
2
3>>>
What happens if we just use type print
without having the parenthesis?
xxxxxxxxxx
21>>> print
2<built-in function print>
We don't get an error. Instead, the message is that print
is a built-in function. But the following code throws an error:
xxxxxxxxxx
51>>> print 'Hello World!'
2 File "<stdin>", line 1
3 print 'Hello World!'
4 ^
5SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello World!')?
The interpreter hits back with a SyntaxError
. Think about the syntax like the grammar of human languages. In the code given above, we have missed the parentheses. The fourth lesson will take up this issue in greater detail.
Before we jump into the serious stuff, let us try and print some emojis!
Try this out in your repl! A full list of emojis can be found here.
Strings like 'Hello World!'
and numbers like 1
, 2.0
are called literals in Python. Formally, a literal is something that describes a constant value. Variables are containers that are used to store values. Variables in Python are defined in the following way:
xxxxxxxxxx
91>>> x = 1
2>>> print(x)
31
4>>> y = 'a string'
5>>> print(y)
6a string
7>>> foo_bar = 123.456
8>>> print(foo_bar)
9123.456
=
is called the assignment operator. Whenever the assignment operator is present in a statement, it is used for one of the following purposes:
xxxxxxxxxx
41>>> x = 1 # define a new variable
2>>> x = x + 1 # update an existing variable
3>>> print(x)
42
The assignment operator is evaluated from right to left. That is, the expression to the right of the assignment operator is evaluated first. This result is then assigned to the variable on the left. Variables will be taken up in greater detail in the lessons of the second chapter.
We will be looking at the following basic data types:
The int
type represents integers. Python provides a command called type
to determine the type of an object:
xxxxxxxxxx
41>>> print(1)
21
3>>> type(1)
4<class 'int'>
The float
type represents real numbers:
xxxxxxxxxx
41>>> print(1.0)
21.0
3>>> type(1.0)
4<class 'float'>
The following is also a valid float literal:
xxxxxxxxxx
21>>> print(1.)
21.0
1.
and 1.0
are one and the same literal.
The str
type represents strings:
xxxxxxxxxx
41>>> print('one')
2one
3>>> type("one")
4<class 'str'>
The bool
type represents boolean values:
xxxxxxxxxx
41>>> print(True)
2True
3>>> type(False)
4<class 'bool'>
Please note that bool
values are case sensitive. That is, true
and false
are not bool
values.
A comment is a line of text that is not executed by the interpreter. Comments begin with the #
symbol. The following are comments:
xxxxxxxxxx
31>>> # This is a comment
2>>> # print(1)
3>>>
As line-2 is a comment, 1
is not printed in the next line. Comments can also come at the end of a line of code:
xxxxxxxxxx
21>>> print(1) # This line is printing the value 1
21
Adding comments is one of the ways to make code more readable. Its use will become clear in subsequent chapters.