Class Notes: Introduction to Python
1. Basic Computer Terms
Before we learn Python, we must understand a few computer words:
Instruction: An instruction is a single command given to a computer to do a specific task.
Program: A program is a set of instructions given to a computer to solve a problem. It is like a recipe for the computer to follow.
Programming Language: Just like we use English or Hindi to talk to each other, we use a "Programming Language" to talk to a computer.
2. What is Python?
Definition: Python is a popular, high-level programming language. It is very easy to read and write.
Developer: It was developed by Guido van Rossum.
Year: It was released in the year 1991.
Why the name "Python"? It is not named after the snake. Guido van Rossum named it after a famous British comedy show called "Monty Python’s Flying Circus" because he wanted a name that was unique and mysterious.
3. Features of Python
Why do students and experts love Python?
Easy to Learn: It uses simple English words.
Free: It is free to download and use.
Case Sensitive: Python treats uppercase (A) and lowercase (a) letters as different.
Interpreted Language: It runs the code line-by-line.
4. Python IDLE
IDLE stands for Integrated Development and Learning Environment.
It is the standard software that comes with Python where we write and run our programs.
Working Modes in Python
There are two ways to work in IDLE:
Interactive Mode:
You type a command and press Enter.
Python gives the result immediately.
It is good for testing small lines of code.
The prompt looks like this:
>>>
Script Mode:
You write the whole program in a file and save it.
Then you run the file to see the result.
It is used for writing long programs.
5. Compiler vs. Interpreter
Computers only understand machine language (0s and 1s). We need a translator to convert our Python code into machine language.
Compiler: It translates the whole program at once into machine language. If there are errors, it shows them all at the end.
Interpreter: It translates the program line-by-line. If there is an error in one line, it stops immediately. Python uses an Interpreter.
6. Tokens
A Token is the smallest unit in a program. It is like a "word" or "punctuation mark" in a sentence.
Types of Tokens:
Identifiers:
These are names given to parts of the program, like variables or functions.
Example:
my_name,age,total.
Literals:
These are fixed values or data.
Example:
10(Number literal),"Hello"(String literal).
Delimiters:
These are special symbols used to separate or organize code.
Example:
( ),[ ],{ },,(comma),:(colon).
7. Data Types
A data type tells Python what kind of value we are using.
int (Integer): Used for whole numbers. Example:
5,100,-20.float: Used for numbers with decimal points. Example:
5.5,3.14.str (String): Used for text. It must be inside quotes. Example:
"Hello",'Apple'.
8. Comments
Comments are notes for the programmer.
The computer ignores them; it does not run them.
In Python, a comment starts with the hash symbol
#.Example:
# This is a comment
9. Variables
A variable is like a container or a box used to store data.
Initializing a Variable: This means creating a variable and putting a value in it.
Syntax:
Variable_Name = ValueExample:
age = 12(Here,ageis the variable and12is the value).
10. Important Functions
print(): It displays the output on the screen.Example:
print("Hello World")
input(): It takes information from the user (keyboard).Example:
name = input("Enter your name: ")
int(): It converts a string (text) or number into an integer. It is often used with input to take numbers.Example:
age = int(input("Enter your age: "))
Exercise Work
In Python, understanding these three components is like learning the grammar and punctuation of a new language. Here is a breakdown of what they are and how they function.
D. Define the following -
1. Token
A token is the smallest individual unit in a Python program. When you run your code, the Python interpreter breaks the lines of text into these basic logical components to understand what you want it to do.
2. Operator
An operator is a special symbol or keyword used to perform computations or logical operations on data (called operands).
3. Comments
Comments are notes written by the programmer to explain what the code is doing. Most importantly, Python completely ignores them during execution. They are strictly for humans.
E. Differentiate between the following -
| Keywords | Literals | |
| Reserved words that have a special, predefined meaning to the Python interpreter. | Notations for representing fixed, constant values in source code. | |
| Used to define the structure, flow, and logic of the code (commands). | Used to provide specific data values to the program. | |
| Immutable. You cannot change their meaning or use them as variable names. | Constant. The value 5 always means the number 5, but you can assign it to different variables. | |
Here are the answers:
1. Who developed Python and when?
Python was developed by Guido van Rossum and was first released in 1991.
2. What do you mean by identifier?
An identifier is a user-defined name given to entities like variables, functions, classes, or modules to distinguish them from one another in the code.
Here are the answers:
3. What are variables?
A variable is a named location in a computer's memory used to store data that can be changed during the program's execution. Think of it as a labeled container for values (e.g., in score = 10, score is the variable).
4. What do you understand by indentation?
Indentation refers to the leading whitespace (spaces or tabs) at the start of a line of code. Python uses indentation to define the structure and nesting of code blocks.
G. Answer briefly -
Ans 1. Python Data Types
Integer (
int): Represents whole numbers (positive, negative, or zero) without any decimal points.Example:
10,-5,0
Float (
float): Represents real numbers that contain a decimal point.Example:
3.14,-0.01,2.0
String (
str): Represents a sequence of characters (text) enclosed in single (') or double (") quotes.Example:
"Hello",'Python',"123"(Note: "123" is text, not a number)
Boolean (
bool): Represents one of two logical values:TrueorFalse. It is often used in conditional testing.Example:
True,False(Note: Capital 'T' and 'F' are required)
2. input() and print() Functions
input(): This function allows the program to accept data from the user through the keyboard. It pauses program execution until the user types something and hits Enter.
It always reads the data as a String, even if the user types a number.
Syntax:
variable = input("Prompt text")
print(): This function displays data (variables, strings, numbers) to the screen (standard output). It is used to show the result of a program to the user.
Syntax:
print("Result:", variable)