0% found this document useful (0 votes)
0 views

Introduction to Python

The document provides an introduction to Python, highlighting its definition, ease of learning, and applications in various fields. It includes instructions for setting up Python, writing basic programs, and understanding fundamental concepts such as variables, data types, and string operations. Additionally, it offers resources for beginners and examples of exercises to practice coding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Introduction to Python

The document provides an introduction to Python, highlighting its definition, ease of learning, and applications in various fields. It includes instructions for setting up Python, writing basic programs, and understanding fundamental concepts such as variables, data types, and string operations. Additionally, it offers resources for beginners and examples of exercises to practice coding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to Python

BCA Y1 2025
By: Debasmita Bhoumik
What is Python?
● Definition: Python is a high-level, interpreted, and versatile programming
language.
● Why Learn Python?
○ Easy to read and write (great for beginners).
○ Widely used in web development, data analysis, artificial intelligence,
automation, and more.
○ Extensive library support.
Setting Up Python
● Download and install Python: python.org/downloads
● Install an Integrated Development Environment (IDE):
○ Beginner-friendly options: IDLE, Thonny.
○ Advanced: VS Code, PyCharm, Jupyter Notebook.

Verify installation using the command:


python --version
Writing Your First Python Program
● Open a text editor or IDE.

Type the following:


print("Hello, World!")


● Run the program:
○ Save it as hello.py.

Execute in the terminal/command prompt:


python hello.py
Python Basics
Variables and Data Types

● Variables store data values.

No need to declare types explicitly (dynamically typed).


name = "Alice" # String
age = 25 # Integer
height = 5.5 # Float
is_student = True # Boolean

Use type() to check the type:


print(type(name))
Python Basics
Comments
Single-line comment:
# This is a comment

Multi-line comment:
"""
This is a
multi-line comment.
"""
Python Basics
Input and Output
Taking user input:
name = input("What is your name? ")
print("Hello, " + name + "!")
Python Basics
Basic Operations
Arithmetic:
x=5
y=3
print(x + y) # Addition
print(x - y) # Subtraction
print(x * y) # Multiplication
print(x / y) # Division

String operations:
greeting = "Hello"
print(greeting + " World!") # Concatenation
print(greeting * 3) # Repetition


Exercise
Write a program that asks the user for their name and greets them:
Exercise
Write a program that asks the user for their name and greets them:

name = input("Enter your name: ")


print("Nice to meet you, " + name + "!")
Simple Calculator

Create a program that adds two numbers provided by the user:


Simple Calculator

Create a program that adds two numbers provided by the user:

num1 = float(input("Enter the first number: "))


num2 = float(input("Enter the second number: "))
print("The sum is:", num1 + num2)
Python Resources for Beginners

● Python Official Documentation


● W3Schools Python Tutorial
● Real Python
Python string:
Strings in python are surrounded by either single quotation marks, or double quotation marks.

'hello' is the same as "hello".

Slicing:
You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return a part of the string.

Example: Get the characters from position 2 to position 5 (not included):

b = "Hello, World!"

print(b[2:5])
Upper Case

The upper() method returns the string in upper case:

a = "Hello, World!"

print(a.upper())
String Concatenation
To concatenate, or combine, two strings you can use the + operator.

Example: Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c=a+b
print(c)
String Format

As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:

Example:

age = 36

txt = "My name is John, I am " + age

print(txt)
String Format

But we can combine strings and numbers by using f-strings or the format() method!

F-String was introduced in Python 3.6, and is now the preferred way of formatting strings.

To specify a string as an f-string, simply put an f in front of the string literal, and add curly brackets {} as placeholders
for variables and other operations.

Example

age = 36

txt = f"My name is John, I am {age}"

print(txt)
For loop
for i in range(1, 6):
for variable in sequence:
print(i)
# Code block to execute for each element in the sequence

Example:
1
fruits = ["apple", "banana", "cherry"]

for fruit in fruits: 2

print(fruit) 3

4
apple
5
banana

cherry
For loop For loop in dictionary
text = "Python"

for char in text:

print(char) person = {"name": "Alice", "age": 25, "city":


"New York"}

for key, value in person.items():


P
print(key, ":", value)
y

t
name : Alice
h
age : 25
o
city : New York
n
Dictionary

Dictionaries are used to store data values in key:value pairs.


A dictionary is a collection which is ordered*, changeable and do not allow duplicates.

thisdict = {

"NAME": "Debasmita",

"SUB": "CSE",

"ID": 878

print(thisdict)

You might also like