0% found this document useful (0 votes)
15 views30 pages

Lecture 01

Uploaded by

Ashwin Ghayar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views30 pages

Lecture 01

Uploaded by

Ashwin Ghayar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Introduction to Computing

Lecture 1: Getting started & basic syntax

Baskar Balasubramanyam
August 2025
IISER Pune

1
Welcome to Python Programming!

• Today we’ll explore the fundamentals of Python


• You’ll write your first Python program
• By the end of this lecture, you’ll understand:
• Basic syntax and structure
• Variables and some data types
• Input and output operations
• import command
• Hands-on programming exercises

2
What is Python?

• High-level, interpreted programming language


• Created by Guido van Rossum in 1991
• Named after Monty Python’s Flying Circus
• The Zen of Python 3: ”Simple is better than complex”
• It is an open-source programming language
• Used by Google, Netflix, Instagram, Spotify, and many more
• Perfect for beginners, powerful for experts

3
Why Learn Python?

Advantages: Applications:
• Easy to read and write • Web development
• Extensive libraries • Data science
• Cross-platform • Machine learning
• Large community • Automation
• Versatile applications • Game development

4
Understanding the Python Interpreter

• Python is an interpreted language, as opposed to compiled


languages like C++, Java
• Code is executed line by line
• No separate compilation step needed
• The interpreter translates Python code to machine code
• Two modes:
• Interactive mode: Execute commands immediately – using
Jupyter Notebook or ipython3
• Script mode: Run entire programs from files

5
Installing Python

• Download from python.org


• Choose Python 3.x (latest version)
• Installation includes:
• Python interpreter
• IDLE (Integrated Development Environment)
• pip (package installer)
• Standard library
• Verify installation: python --version

6
Running python3 - Interactive Mode

• Open terminal/command prompt


• Type python3
• You’ll see the Python prompt: >>>

$ python3
Python 3.12.0 ( main , Oct 2 2023 , 20:42:31)
[ GCC 9.4.0] on linux
Type " help " , " copyright " , " credits " or " license " for
more info .
>>> print ( " Hello , World ! " )
Hello , World !
>>> 2 + 3
5
>>> exit ()

7
Running ipython3 - Interactive Mode

You can alternatively run ipython3 instead of python3


$ ipython3
Python 3.9.6 ( default , Apr 30 2025 , 02:07:17)
Type ’ copyright ’ , ’ credits ’ or ’ license ’ for more
information
IPython 8.18.1 -- An enhanced Interactive Python . Type
’? ’ for help .

In [1]: print ( " Hello , World ! " )


Hello , World !

In [2]: 2+3
Out [2]: 5

In [3]: exit ()

8
Running Python - Script Mode

• Create a file with .py extension


• Write Python code in the file
• Run with: python filename.py

hello.py:
print ( " Hello , World ! " )
print ( " Welcome to Python programming ! " )

Running the script:


$ python3 hello . py
Hello , World !
Welcome to Python programming !
$

9
Your First Python Program

Let’s create the classic ”Hello, World!” program:


# This is my first Python program
print ( " Hello , World ! " )

• Line 1: A comment (starts with #)


• Line 2: The print() function displays text
• Save as hello.py and run it!

10
Python Syntax Rules

• Case sensitive: Print ̸= print


• Indentation matters: Used to define code blocks
• No semicolons: Line endings are natural breaks
• No curly braces: Indentation replaces {}
• Comments: Start with #
• Strings: Use single ’ or double " quotes

11
Indentation - The Python Way

Other languages:
Python:
if ( x > 0) {
if x > 0:
printf ( " Positive " ) ;
print ( " Positive " )
printf ( " Number " ) ;
print ( " Number " )
}

• Use 4 spaces for indentation (recommended)


• All lines at the same level must have the same indentation
• IndentationError occurs if indentation is wrong

12
Comments in Python

# This is a single - line comment


print ( " Hello ! " ) # Comment at end of line

# Multi - line comments can be written


# using multiple single - line comments
# like this

"""
This is a multi - line string
that can serve as a comment
for documentation purposes
"""

• Comments are ignored by the interpreter


• Use them to explain your code
• Good comments make code readable
13
The print() Function

print ( " Hello , World ! " )


print ( ’ Single quotes work too ’)
print ( " You can print " , " multiple " , " items " )
print ( " Number : " , 42)
print ( " Sum : " , 2 + 3)

# Controlling output
print ( " Hello " , end = " " )
print ( " World ! " ) # Prints : Hello World !
print ( " A " , " B " , " C " , sep = " -" ) # Prints : A -B - C

• print() displays output to the screen


• Can print text, numbers, and expressions
• end="" controls line ending
• sep="" controls separator between items
14
Variables in Python

# Creating variables
name = " Alice "
age = 20
height = 5.6
is_student = True

# Using variables
print ( " Name : " , name )
print ( " Age : " , age )
print ( " Height : " , height )
print ( " Student : " , is_student )

• Variables store data values


• No need to declare type explicitly
• Variable names must start with letter or underscore
• Can contain letters, numbers, and underscores
15
Variable Naming Rules

Invalid names:
Valid names:
2 name # starts with
name
number
age
first - name # contains
first_name
hyphen
_private
class # reserved
count2
keyword
userName
my . var # contains dot

Conventions:

• Use lowercase with underscores: my variable


• Be descriptive: student count not sc
• Avoid reserved keywords

16
Getting Input from Users

# Basic input
name = input ( " What ’s your name ? " )
print ( " Hello , " , name )

# Input is always a string


age_str = input ( " How old are you ? " )
age = int ( age_str ) # Convert to integer
print ( " You are " , age , " years old " )

# One - liner conversion


height = float ( input ( " Your height in feet : " ) )
print ( " Height : " , height )

• input() always returns a string


• Use int(), float() to convert. We will see more about data
types in the next lecture
• The prompt message is optional 17
Hands-on Exercise 1: Enhanced Hello World

Task: Create a program that asks for the user’s name and age,
then provides a personalized greeting.
# Get user information
name = input ( " What ’s your name ? " )
age = int ( input ( " How old are you ? " ) )

# Create personalized greeting


print ( " Hello , " , name + " ! " )
print ( " You are " , age , " years old . " )
print ( " Nice to meet you ! " )

# Bonus : Calculate birth year


current_year = 2025
birth_year = current_year - age
print ( " You were born in " , birth_year )

18
Hands-on Exercise 2: Basic Calculator

Task: Create a simple calculator that performs basic arithmetic


operations.
# Simple Calculator
print ( " Welcome to the Python Calculator ! " )

# Get two numbers from user


num1 = float ( input ( " Enter first number : " ) )
num2 = float ( input ( " Enter second number : " ) )

# Perform calculations
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2

19
Hands-on Exercise 2: Basic Calculator (cont’d)

Task: Create a simple calculator that performs basic arithmetic


operations.
# Display results
print ( " Results : " )
print ( f " { num1 } + { num2 } = { addition } " )
print ( f " { num1 } - { num2 } = { subtraction } " )
print ( f " { num1 } * { num2 } = { multiplication } " )
print ( f " { num1 } / { num2 } = { division } " )

20
String Formatting

# Different ways to format strings


name = " Alice "
age = 25
score = 95.5

# Method 1: + concatenation
print ( " Name : " + name + " , Age : " + str ( age ) )

# Method 2: Comma separation


print ( " Name : " , name , " Age : " , age )

# Method 3: . format () method


print ( " Name : {} , Age : {} " . format ( name , age ) )

# Method 4: f - strings ( Python 3.6+)


print ( f " Name : { name } , Age : { age } " )
print ( f " Score : { score :.1 f }% " ) # One decimal place
21
String Formatting (cont’d)

• f-strings are the most modern and readable


• Can format numbers, dates, and more
• We will look at this in greater detail in the next lecture

22
Common Mistakes and Debugging

# Common Error 1: Forgetting quotes


print ( Hello ) # NameError : name ’ Hello ’ is not defined
print ( " Hello " ) # Correct

# Common Error 2: Wrong indentation


print ( " Start " )
print ( " Indented " ) # IndentationError

# Common Error 3: Type mismatch


age = input ( " Age : " ) # Returns string
print ( age + 1) # TypeError : can ’t concatenate str and
int
print ( int ( age ) + 1) # Correct

# Common Error 4: Undefined variable


print ( name ) # NameError if name not defined
name = " Alice "
print ( name ) # Correct 23
Practice Problem: Personal Information Manager

Task: Create a program that collects and displays personal


information in a formatted way.
# Personal Information Manager
print ( " === Personal Information Manager === " )

# Collect information
first_name = input ( " First name : " )
last_name = input ( " Last name : " )
age = int ( input ( " Age : " ) )
city = input ( " City : " )
favorite_color = input ( " Favorite color : " )

# Calculate additional info


full_name = first_name + " " + last_name
decades = age // 10

# Display formatted information 24


Importing Packages in Python

• Python uses the import statement to bring external


functionality into your script.
• Three common patterns:
• import math — import the entire module, then access with
math.
• import math as m — same as above, but with the shorter
alias m.
• from math import sqrt, pi — import only the names you
need.
• Third-party packages must be installed first (e.g.
pip install numpy) before they can be imported.

25
Example: Working with math

import math # whole - module import

radius = 3.0
area = math . pi * math . pow ( radius , 2)
print ( f " Circle area = { area :.2 f } " )

from math import sqrt , factorial # selective import

x = 144
y = 5
print ( " sqrt ( x ) = " , sqrt ( x ) ) # 12.0
print ( f " { y }! = " , factorial ( y ) ) # 120

26
Best Practices for Python Programming

• Use meaningful variable names


• student count instead of sc
• temperature instead of temp
• Write comments to explain your code
• Use consistent indentation (4 spaces)
• Test your code frequently
• Handle user input carefully

27
Should I learn python in the age of AI?

• This is like asking Why should I learn math when I can


just use a calculator?
• AI amplifies your capabilities:
• Use AI to write code faster, but you need to understand it
• Debug AI-generated code and customize it for your needs
• Popular AI frameworks are Python-based:
• TensorFlow, PyTorch, Scikit-learn, OpenAI API are python
based
• Python is the #1 language for AI/ML development
• Understanding code helps you work with AI tools effectively

28
Today’s Summary

You’ve learned:

• How to run Python programs (interactive and script mode)


• Basic syntax rules and the importance of indentation
• How to use variables and basic data types
• Input and output operations with input() and print()
• How to create simple programs

29
Questions and Discussion

Questions?

Next topic: Working with data types

30

You might also like