Lesson-1.1
Python shell | Replit Console
In this lesson, we will be working with the Python interpreter in Interactive Mode. It is also often called the 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:
Prompts
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:
Fire up a repl and type the code in the console. You will be getting the output on the next line.
Output
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.
The sentence enclosed by the parentheses 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:
Run the code given above and observe the output. print()
can also be used to print numbers:
Multiple items can be printed on the same line in the following way:
Notice the presence of a space between successive elements? print
automatically seperates multiple values with a delimiter, which is space by default.
If the print
command is called without passing any input to it, then it prints a blank line:
What happens if we just use type print
without having the parentheses?
We don't get an error. Instead, the message is that print
is a built-in function. But when you try the following code:
>>> print 'Hello World!'
File "<stdin>", line 1
print 'Hello World!'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello World!')?
The interpreter hits back with a SyntaxError
. Like human languages, programming languages have their own syntaxes that have to be followed to convey your commands to the computer. Programming language syntaxes, however, are far more strict and have to be followed exactly in order to run your code. Parantheses are used to execute and pass values (called arguments in programming jargon) to functions like print
. Note the lack of parentheses in the above snippet which is also pointed out by the handy error message. We will learn the syntax for various concepts in python in the upcoming lessons.
Emojis
Before we jump into the serious stuff, let us try and print some emojis!
>>> print('\N{smiling face with smiling eyes}')
😊
>>> print('\N{grinning face}')
😀
>>> print('\N{smiling face with halo}')
😇
>>> print('\N{thinking face}')
🤔
Try this out in an interpreter of your choice! A full list of emojis can be found here.
Literals and Variables
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:
>>> x = 1
>>> print(x)
1
>>> y = 'a string'
>>> print(y)
a string
>>> foo_bar = 123.456
>>> print(foo_bar)
123.456
=
is called the assignment operator. Whenever the assignment operator is present in a statement, it is used for one of the following purposes:
- define a new variable
- update an existing variable
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.
Basic Data Types | type()
We will be looking at the following basic data types:
- Integer
- Float
- String
- Boolean
Integer
The int
type represents integers. Python provides a command called type
to determine the type of an object:
Float
The float
type represents real numbers:
The following is also a valid float literal:
1.
and 1.0
are one and the same literal.
String
The str
type represents strings:
Boolean
The bool
type represents boolean values:
Please note that bool
values are case sensitive. That is, true
and false
are not bool
values.
Comments
A comment is a line of text that is not executed by the interpreter. Comments begin with the #
symbol. The following are comments:
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:
Adding comments is one of the ways to make code more readable. Its use will become clear in subsequent chapters.