CSE23CL203 Lecture1 IntroPython DataTypes
CSE23CL203 Lecture1 IntroPython DataTypes
Exam Tip
Interactive Mode = quick thinking. VS Code = professional habit. You need both.
l Industry Connection
Almost every software job – web apps, AI models, automated reports – is written as text files full
of variables and simple statements exactly like the ones in this lecture. There is no “jump ahead” –
everyone starts here.
Programming Language
A programming language is a set of instructions, written in a fixed vocabulary and grammar, that a
computer can follow exactly. There is no guessing what you meant – the computer does precisely what
is written.
Why Python?
■ High-level: reads almost like plain English, hides low-level machine details.
■ Interpreted: code runs line by line via an interpreter (no separate compile step needed before testing).
■ Dynamically typed: no need to declare data types explicitly.
■ Huge collection of ready-made tools (libraries) for data analysis, automation, web development, and AI.
Feature Highlights
■ Simple & readable syntax – close to English, minimal punctuation.
■ Free & open-source – no license cost, large community.
■ Portable – same code runs on Windows, Linux, macOS.
■ Extensive standard library – “batteries included” (file handling, math, dates, etc. built in).
■ Object-oriented – supports organizing code into classes and objects (Unit 2).
Exam Tip
Remember Python is both interpreted (runs step by step) and dynamically typed (types decided auto-
matically) – a common two-mark question.
Interpreter
The interpreter is the program that reads your Python code and executes it. Installing “Python” on
your machine means installing this interpreter (from [Link], or bundled inside tools like Ana-
conda/Jupyter).
Script Mode
Write code in a .py file, then run the whole file at Interactive Mode
once: Run code cell-by-cell or line-by-line in Jupyter
Notebook/IDLE and see results immediately – best
python hello .py
for learning and experimenting.
Best for programs you will reuse.
Say Hello
Type this in a file (say [Link]) or an interactive cell:
Say Hello
Type this in a file (say [Link]) or an interactive cell:
Q1
Python code runs line by line through an interpreter, without a separate compile step. What is this
behaviour called?
Q1
Python code runs line by line through an interpreter, without a separate compile step. What is this
behaviour called?
Answer
Interpreted execution.
Q2
True or False: You must write int age = 20 in Python, declaring the type before the variable name.
Q2
True or False: You must write int age = 20 in Python, declaring the type before the variable name.
Answer
False. Python uses dynamic typing – just write age = 20; Python infers the type automatically.
Answers
1. python [Link]
2. print()
3. False – dynamic typing
4. #
5. Interactive Mode (Jupyter Notebook / IDLE)
Exercise 1
Write one line of code that prints Welcome to Python Lab.
Exercise 1
Write one line of code that prints Welcome to Python Lab.
Hint
Which function displays text on screen? What punctuation must surround plain text?
Exercise 1
Write one line of code that prints Welcome to Python Lab.
Hint
Which function displays text on screen? What punctuation must surround plain text?
Solution
Exercise 2
You need to write a 50-line program that you will run many times over the semester. Should you use
Script Mode or Interactive Mode? Justify in one line.
Exercise 2
You need to write a 50-line program that you will run many times over the semester. Should you use
Script Mode or Interactive Mode? Justify in one line.
Hint
Which mode lets you save a program permanently and re-run it as a whole, instead of typing it fresh
each time?
Exercise 2
You need to write a 50-line program that you will run many times over the semester. Should you use
Script Mode or Interactive Mode? Justify in one line.
Hint
Which mode lets you save a program permanently and re-run it as a whole, instead of typing it fresh
each time?
Solution
Script Mode – save it as a .py file so it can be re-run as a whole, instead of re-typing it cell by cell.
Variable
A variable is a name that refers to a value stored in memory. Think of it as a labelled box: the label is
the variable name, and the box holds the value.
age = 20
name = " Priya "
height = 5.4
Reading Assignment
age = 20
Read right to left: “Take value 20, store it in box age.” The = is assignment, not mathematical equality.
Identifier
An identifier is the name given to a variable, function, class, or any other user-defined item in Python.
Naming Rules
■ Must start with a letter or underscore ( ), not a digit.
■ Can contain letters, digits, underscores – no spaces or symbols (-, @, etc.).
■ Case-sensitive: Age and age are different identifiers.
■ Cannot be a Python keyword (reserved word like if, for, class).
■ Convention: lowercase with underscores, e.g. student name (snake case).
Common Mistake
2nd value = 10 ⇒ SyntaxError – identifiers cannot start with a digit.
Output
Output is information the program displays. The print() function is Python’s main output tool.
name = "Arun"
marks = 88
print ("Name:", name , " Marks :", marks )
Explanation
print() can take several items separated by commas – it automatically prints them in order, separated
by a single space.
f-string
An f-string is written with a lowercase f right before the opening quote: f"...". It places a variable’s
value directly inside the text.
f-string
An f-string is written with a lowercase f right before the opening quote: f"...". It places a variable’s
value directly inside the text.
The f tells Python to look inside {} and substitute the variable’s value there.
f-string
An f-string is written with a lowercase f right before the opening quote: f"...". It places a variable’s
value directly inside the text.
The f tells Python to look inside {} and substitute the variable’s value there.
Input
Input is data the program receives from the user, using the input() function. input() always returns
a string, even if the user types a number.
Worked Example
Common Mistake
age + 1 directly ⇒ TypeError, because age is a string until converted with int().
Expected Output
Expected Output
Explanation
■ input() always returns a string, so m1 and m2 are "78" and "92" until converted with int().
■ Without int(), m1 + m2 would concatenate the strings ("7892") instead of adding numbers – or raise a
TypeError for m1 / 2.
■ The result avg is a float because / always performs true division.
Q1
Which of these are valid Python identifiers? 2marks, marks, student-name, Student1
Q1
Which of these are valid Python identifiers? 2marks, marks, student-name, Student1
Answer
Valid: marks, Student1. Invalid: 2marks (starts with a digit), student-name (hyphen not allowed).
Q2
Predict the output: name = "Sam"
print(f"Hi {name}!")
Q2
Predict the output: name = "Sam"
print(f"Hi {name}!")
Answer
Hi Sam!
Answers
1. name = "Arun"
2. input()
3. Curly braces {} inside an f"..." string
4. No – identifiers cannot start with a digit
5. str
Exercise 1
Take a student’s name and roll number as input, and print: Roll No 21: Divya (using an f-string).
Exercise 1
Take a student’s name and roll number as input, and print: Roll No 21: Divya (using an f-string).
Hint
Use input() twice, then build the sentence with an f-string and curly-brace placeholders.
Exercise 1
Take a student’s name and roll number as input, and print: Roll No 21: Divya (using an f-string).
Hint
Use input() twice, then build the sentence with an f-string and curly-brace placeholders.
Solution
Exercise 2
Find and correct the errors: 2nd name = "Arun" and class = 10.
Exercise 2
Find and correct the errors: 2nd name = "Arun" and class = 10.
Hint
Recall the two identifier rules broken here: what can’t a name start with, and what word can’t be used
as a name?
Exercise 2
Find and correct the errors: 2nd name = "Arun" and class = 10.
Hint
Recall the two identifier rules broken here: what can’t a name start with, and what word can’t be used
as a name?
Solution
Exercise 3
Write a program that asks for two numbers using input() and prints their sum as an integer.
Exercise 3
Write a program that asks for two numbers using input() and prints their sum as an integer.
Hint
Remember: input() always returns a string – what must you do before adding two of them as numbers?
Exercise 3
Write a program that asks for two numbers using input() and prints their sum as an integer.
Hint
Remember: input() always returns a string – what must you do before adding two of them as numbers?
Solution
Overview
Type Meaning Example
int marks = 78
Integer: whole number, no decimal
point
float cgpa = 8.5
Floating-point number: has a deci-
mal point
complex Has a real and imaginary part (rarely 3 + 4j
used in this course)
Dynamic Typing
Python figures out the data type automatically from the value assigned – called dynamic typing. You
never write “this is an integer” explicitly, unlike languages such as C or Java.
a = 17; b = 5
print (a + b, a - b, a * b)
print (a / b) # true division -> float
print (a // b) # floor division -> int
print (a % b) # remainder
print (a ** b) # exponent ( power )
a = 17; b = 5
print (a + b, a - b, a * b)
print (a / b) # true division -> float
print (a // b) # floor division -> int
print (a % b) # remainder
print (a ** b) # exponent ( power )
Solution
22 12 85 3.4 3 2 1419857
Exam Tip
/ always gives a float; // (floor division) gives the whole-number quotient, discarding the remainder.
String
A string (str) is a sequence of characters enclosed in single ’...’ or double "..." quotes. Strings
are indexed – each character has a position number starting at 0.
Slicing
Slicing means extracting a sub-part of a string using start:stop. The character at stop is not
included.
Expected Output
Hands
Lab
HANDS -ON PYTHON LAB
[’Hands -on ’, ’Python ’, ’Lab ’]
Expected Output
Hands
Lab
HANDS -ON PYTHON LAB
[’Hands -on ’, ’Python ’, ’Lab ’]
Explanation
■ s[0:5] takes indices 0 to 4 – “Hands” (5 characters, index 5 not included).
■ s[-3:] counts 3 characters back from the end – “Lab”.
■ .upper() converts every letter to uppercase; the hyphen is unaffected.
■ .split() with no argument splits on whitespace, returning a list of 3 words.
Q1
What is 17 // 5 in Python?
Q1
What is 17 // 5 in Python?
Answer
3 – floor division discards the remainder.
Q2
What does "PYTHON"[2:5] return?
Q2
What does "PYTHON"[2:5] return?
Answer
’THO’ – indices 2, 3, 4 (index 5 not included).
Q3
True or False: Strings in Python are mutable (their characters can be changed in place).
Q3
True or False: Strings in Python are mutable (their characters can be changed in place).
Answer
False. Strings are immutable – s[0] = "X" raises a TypeError.
Answers
1. //
2. **
3. s[2:5]
4. [Link]()
5. len(s)
Exercise 1
Given three integer marks 78, 85, 91, print their total and average (rounded using //, and then using
/).
Exercise 1
Given three integer marks 78, 85, 91, print their total and average (rounded using //, and then using
/).
Hint
Add the three marks first, then apply // for a whole-number average and / for a decimal one.
Exercise 1
Given three integer marks 78, 85, 91, print their total and average (rounded using //, and then using
/).
Hint
Add the three marks first, then apply // for a whole-number average and / for a decimal one.
Solution
m1 , m2 , m3 = 78, 85, 91
total = m1 + m2 + m3
print (" Total :", total )
print (" Average (int):", total // 3)
print (" Average ( float ):", total / 3)
Exercise 2
Given s = "Data Analytics", print the string reversed and its length.
Exercise 2
Given s = "Data Analytics", print the string reversed and its length.
Hint
Slicing with a step of -1 reverses a string. Which built-in function gives the length?
Exercise 2
Given s = "Data Analytics", print the string reversed and its length.
Hint
Slicing with a step of -1 reverses a string. Which built-in function gives the length?
Solution
Exercise 3
Given price = "499.99" (a string), print it rounded to the nearest whole number.
Exercise 3
Given price = "499.99" (a string), print it rounded to the nearest whole number.
Hint
First convert the string to a float, then use Python’s built-in rounding function.
Exercise 3
Given price = "499.99" (a string), print it rounded to the nearest whole number.
Hint
First convert the string to a float, then use Python’s built-in rounding function.
Solution
Type Conversion
Type conversion (casting) means converting a value from one data type to another using int(),
float(), str(), bool().
Common Mistake
age = "20"
next_year = age + 1 # TypeError !
You cannot add a number directly to a string, even if it “looks like” a number. Convert first: int(age)
+ 1.
Worked Example
Worked Example
Solution
<class ’int’> <class ’float’> <class ’str’> <class ’bool’>
Exam Tip
bool is technically a subtype of int: True == 1 and False == 0 evaluate to True.
Q1
What error occurs when you run "5" + 5?
Q1
What error occurs when you run "5" + 5?
Answer
TypeError – you cannot add a str and an int directly; convert first with int("5") or str(5).
Q2
Predict the output: print(int("10") + int("20"))
Q2
Predict the output: print(int("10") + int("20"))
Answer
30 – both strings are converted to int before the addition.
Answers
1. int()
2. type(x)
3. "45" (a string)
4. False
5. 10
Exercise 1
Convert the integer 45 to a string and print "Weight: 45 kg" by concatenating.
Exercise 1
Convert the integer 45 to a string and print "Weight: 45 kg" by concatenating.
Hint
You cannot join a string and an integer with + directly – which function turns a number into text first?
Exercise 1
Convert the integer 45 to a string and print "Weight: 45 kg" by concatenating.
Hint
You cannot join a string and an integer with + directly – which function turns a number into text first?
Solution
weight = 45
print (" Weight : " + str( weight ) + " kg")
Exercise 2
Take a person’s age as input (a string by default), convert it to an integer, and print "Adult" if age is 18
or above, else "Minor".
Exercise 2
Take a person’s age as input (a string by default), convert it to an integer, and print "Adult" if age is 18
or above, else "Minor".
Hint
Convert the input to int first, then compare it against 18 using >=.
Exercise 2
Take a person’s age as input (a string by default), convert it to an integer, and print "Adult" if age is 18
or above, else "Minor".
Hint
Convert the input to int first, then compare it against 18 using >=.
Solution
(if-else is formally covered in the next module – this previews the idea.)
Python Basics
Type Conversion
Input/Output Conversion
Slicing
input() int(), float(),
s[start:stop]
print() str(), bool()
Reflect
In one sentence: “Why does input() always return a string, even when the user types a number?”
Reference
These exercises are in the style of NCERT Class XI Computer Science (Getting Started with Python;
Data Handling chapters), extending what you have just practiced with the kind of questions asked at the
school level – useful for revisiting fundamentals.
Exercise
Classify the following into their Python data types: 25, 25.5, "Delhi", True, 3+4j.
Exercise
Classify the following into their Python data types: 25, 25.5, "Delhi", True, 3+4j.
Hint
Check for a decimal point, quotes, True/False, and the letter j.
Exercise
Classify the following into their Python data types: 25, 25.5, "Delhi", True, 3+4j.
Hint
Check for a decimal point, quotes, True/False, and the letter j.
Solution
25 → int, 25.5 → float, "Delhi" → str, True → bool, 3+4j → complex (a number type NCERT
introduces alongside int/float).
Exercise
What will be the output of the following code?
a = 5
b = 2
print (a / b)
print (a // b)
print (a % b)
Exercise
What will be the output of the following code?
a = 5
b = 2
print (a / b)
print (a // b)
print (a % b)
Hint
Recall: / always gives a float, // gives the whole-number quotient, % gives the remainder.
Exercise
What will be the output of the following code?
a = 5
b = 2
print (a / b)
print (a // b)
print (a % b)
Hint
Recall: / always gives a float, // gives the whole-number quotient, % gives the remainder.
Solution
2.5 2 1
Exercise
A variable x is assigned the value 10, and later in the same program it is assigned the value "ten". Is
this allowed in Python? What is this behaviour called?
Exercise
A variable x is assigned the value 10, and later in the same program it is assigned the value "ten". Is
this allowed in Python? What is this behaviour called?
Hint
Think about whether Python fixes a variable’s type permanently at creation.
Exercise
A variable x is assigned the value 10, and later in the same program it is assigned the value "ten". Is
this allowed in Python? What is this behaviour called?
Hint
Think about whether Python fixes a variable’s type permanently at creation.
Solution
Yes, this is allowed. A variable can be reassigned to a value of a different data type at any time – this
flexibility is called dynamic typing.
Coming Up
In Lecture 2, we move from single values to collections of values: Lists, Tuples, Dictionaries, and Sets
– how to store many items in one variable, and how to access, modify, and choose the right structure for
a task.
Pre-Read Prompt
Before Lecture 2, think: what real-life “list” do you keep (e.g., shopping list, to-do list)? Can it have
duplicate items? Does order matter?
Allen B. Downey, Think Python: How to Think Like a Computer Scientist, 2nd Ed., Green Tea Press,
2015.
Charles R. Severance, Python for Everybody, 1st Ed., Shroff Publishers, 2017.