Module 3: Learning the Basics of Python
So… you’ve got Python and VS Code installed. If not, check this out. This module will talk about how you can start writing your first programs. You’ll learn how to take inputs, print something, the different data types available, change from one type to another, math, and a little about objects.
First, let’s learn how to print outputs. We’ll use an in-built function print() for this. You can print simple sentences, numbers, and almost anything. The basic syntax is:
print("Hello")
And the output is:
Hello
Some other examples are:
Code:
print("How","are","you?",sep="..")
Output:
How..are..you?
By default, the print statement starts printing from a new line. So, the output of
print("Hello")
print("How are you?")
will be
Hello
How are you?
We can use an option called end to print in the same line. For input:
print("How","are","you",sep=" ",end="?")
print("I am fine!")
The output will be:
How are you?I am fine!
Practice Question #1
What will be the output of:
print("This","is","a sample",end=".")
print("Continuing the message.")
print("This is the end")
Now, let’s talk about input. An input statement has three parts.
variable=input("prompt")
- A variable refers to a value stored in memory. In the statement above, variable can be replaced with any name you choose.
- The input() function reads one line of input from the user. A function is a named, reusable block of code that performs a task when called. The input is stored in the computer’s memory and can be accessed later using the variable.
- A prompt is a short message that indicates the program is waiting for input. Here, prompt can be replaced with any message.
Let’s take an example. Suppose the code is:
name=input("Enter your name.")
like=input("What do you like?")
print(name," likes ",like)
The output will be:
Enter your name. Sloth
What do you like? coding
Sloth likes coding
Here, Sloth and coding are my inputs. The variables in the above code were name and like. The = operator assigns a value to a variable. There are some rules for naming variables. Like, the name can have letters, digits and underscores, but it cannot start with a digit. Also, Python is case-sensitive, so Name is different from name. And, you can’t use any keyword as a variable name.
The format a language uses to represent data is known as a data type. The two primary data types in Python are integers (int) and floating-point numbers (float), also known as decimals. Normal arithmetic operators are used here, too. For example, +, -, * and /. The operator precedence is the same as we use in maths. So, first parentheses, then exponentiation (the ** operator), then positive or negative, followed by division, multiplication, and finally, addition and subtraction.
Let’s talk about strings next. A string is a sequence of characters enclosed by matching single or double quotes. For example, “Hi” and ‘Hi’ are both strings. If you want to include single quotes in your strings, enclose them within double quotes, and vice versa. For example, “Don’t go there” and ‘He said, “Hi,” to me’. Or you can use backslashes, like “I\’ll try my best”.
A common operation on a string is to get its length, or the number of characters it contains. This can be achieved by the len() function.
Code:
name="Sloth is a genius"
print(len(name))
Output:
17
As you can see, blank spaces are considered characters too. Next, let’s talk about concatenation. This is an operation that combines two or more strings with the + operator. “A “+”part” produces “A part”.
Practice Question #2
Write a piece of code that takes your first and last names as input separately, and prints the number of letters in your first name, and your full name with spaces in between.
Now, making mistakes in programming is natural. Happens to the best of us. A computer requires very specific instructions to perform tasks. Otherwise, it raises an error. Fortunately, Python always states the line number, type, and other additional details of the error. The main types of errors are syntax errors, logical errors, and runtime errors.
A syntax error occurs in Python when the computer is unable to understand the code due to the code violating Python language rules, such as inappropriate indentation, erroneous keyword usage, or incorrect operator use. A runtime error occurs when the program is executing and encounters an unexpected condition that prevents it from continuing. Runtime errors are also known as exceptions and can occur for various reasons, such as division by zero, attempting to access an index that is out of range, or calling a function that does not exist.
A logical error occurs when the code runs without any syntax or runtime errors but produces incorrect results due to flawed logic in the code. Unlike syntax or runtime errors, these can be challenging to detect and fix because the code runs without producing any error messages. The results may seem correct, but the code might produce incorrect output in certain situations.
Practice Question #3
Identify the type of error:
x=5
y=10
z=x+w
print(z)
Python comes with an extensive standard library of modules. A module is previously written code that can be imported into a program. The import statement defines a variable for accessing code in a module. Import statements often appear at the beginning of a program. The standard library also defines built-in functions such as print(), input(), and float(). A built-in function is always available and does not need to be imported. The complete list of built-in functions is available in Python’s official documentation. A commonly used module in the standard library is the math module. This module defines functions such as sqrt() (square root). Let’s calculate the square root of 25.
import math
root=math.sqrt(25)
print("The square root of 25 is ",root)
The output in this case will be
The square root of 25 is 5.0
An object is a single unit of data in a Python program. Till now, we have seen three types of objects: strings, integers, and floats. As we know, a string is a sequence of zero or more characters. Each character has an index that refers to the character’s position. Indexes are numbered from left to right, starting at 0. Or, you can start from the right (-1). So, in a string “abcdefg”, the position of ‘e’ can either be 4 or -3.
Every object has an identity, a type, and a value:
- An object’s identity is a unique integer associated with the object. Generally, this integer refers to the
memory location where the object is stored. Once created, an object’s identity never changes. The built-in
id() function returns the object’s identity. - An object’s type determines the possible values and operations of an object. Ex: Integers and floats can be
“divided” using the / operator, but strings cannot. The built-in type() function returns the object’s type. - An object’s value represents the current state of the object. Many objects, such as numbers and strings,
cannot be modified once created. Some objects, such as lists (introduced later), are designed to be
modified.
Before ending this module, let’s quickly go through lists and tuples. A list object can be used to bundle elements together in Python. It can be defined using square brackets, with values separated by commas. Ex: a=[1,2,3,4]. Lists can have numbers and strings together. They also have indices starting from 0. So, a[2] is 3. A tuple is similar, but it is defined within (), and can’t be changed after creation, unlike lists.
That’s it for today. Covering too much at once would be detrimental. Go through the topics and don’t hesitate to voice your doubts in the comments. The answers to the practice questions will be revealed in the next module. Next time, we’ll learn about loops and conditional statements.
Discover more from Ge-erdy Verse
Subscribe to get the latest posts sent to your email.
May 29, 2025 @ 5:16 am
Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
May 29, 2025 @ 1:31 pm
Thanks a lot!!! We’ll keep posting more like this, so make sure you sign up!