0% found this document useful (0 votes)
2 views53 pages

Python_introduction

The document is an introduction to Python programming, covering its architecture, types of translators, and key features such as its interpreted nature and ease of use. It discusses Python's applications in various fields, differences between Python 2 and 3, installation steps, syntax, data types, and various data structures like lists, tuples, sets, and dictionaries. Additionally, it includes examples of code, methods, and interview questions related to Python programming.

Uploaded by

I M Hi-MAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views53 pages

Python_introduction

The document is an introduction to Python programming, covering its architecture, types of translators, and key features such as its interpreted nature and ease of use. It discusses Python's applications in various fields, differences between Python 2 and 3, installation steps, syntax, data types, and various data structures like lists, tuples, sets, and dictionaries. Additionally, it includes examples of code, methods, and interview questions related to Python programming.

Uploaded by

I M Hi-MAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Introduction to

PYTHON
By
Jiten Sahoo

08/03/2025
Architecture of Programming

08/03/2025
Types of Translators
Translators can be any of:
• Interpreters
• Compilers
• A hybrid of Interpreters and Compilers
• Assemblers( c prog)

08/03/2025
Interpreter and Compiler
• A compiler translates the entire source code into machine code before execution, resulting
in faster execution since no translation is needed during runtime. On the other hand, an
interpreter translates code line by line during execution, making it easier to detect errors but
potentially slowing down the program.
• Python is a high level language, hence no need of Compiler. It has only interpretor.
• Interpreted in simple terms means running code line by line. It also means that the
instruction is executed without earlier compiling the whole program into machine language.
• Python works as an interpreted language. Consider a scenario where you are trying to run a
python code, but unfortunately, you have made some mistakes at the bottom of the code.
You will find that there is an error generated for obvious reasons, but along with the error,
you will find the output of the program till the line of the program is correct. This is possible
because Python reads the code line by line and generates output based on the code.
Whenever it finds any error in the line, it stops running and generates an error statement.
08/03/2025
Hybrid Translators

• A hybrid translator is a combination of the Interpreter and Compiler. A


popular hybrid programming language is Java. Java first compiles your
source code to an intermediate format known as the Bytecode.

08/03/2025
Introduction to Python
• Python is a popular programming language. It was created by
Guido van Rossum, and released in 1991.
It is used for:
• web development (server-side),
• software development/Web development
• Mathematics
• system scripting and Automation
• Data analysis and machine learning
• Software testing
• Everyday tasks
08/03/2025
What can Python do?
Python is commonly used for developing websites and software, task
automation, data analysis, and data visualisation. Since it’s relatively
easy to learn, Python has been adopted by many non-programmers,
such as accountants and scientists, for a variety of everyday tasks,
like organising finances.
• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify
files.
• Python can be used to handle big data and perform complex
mathematics.
• Python can be used for rapid prototyping, or for production-ready
software development
08/03/2025
Why is Python so popular?
• Python is popular for several reasons. Here's a deeper look at what makes
it versatile and easy for coders to use.
• It has a simple syntax that mimics natural language, so it’s easier to read
and understand. This makes it quicker to build projects and faster to
improve on them.
• It’s versatile. Python can be used for many different tasks, from web
development to machine learning.
• It’s beginner friendly, making it popular for entry-level coders.
• It’s open source, which means it’s free to use and distribute, even for
commercial purposes.
• Python’s archive of modules and libraries—bundles of code that third-party
users have created to expand Python’s capabilities—is vast and growing.
08/03/2025
Python Syntax compared to other programming languages

• Python was designed for readability, and has some similarities to the
English language with influence from mathematics.
• Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
• Python relies on indentation, using whitespace, to define scope; such
as the scope of loops, functions and classes. Other programming
languages often use curly-brackets for this purpose.

08/03/2025
Differences between Python 2 and 3
• Unicode. By default, Python 3 encodes strings using Unicode rather than ASCII.
• Range functions. Python 3’s range() function replaced Python 2’s xrange() function, improving performance when
iterating over sequences.
• Exception handling. Python 3’s exceptions are enclosed in parentheses, while Python 2’s exceptions are enclosed in
notations.
• Integer division. In Python 3, the result of an integer division is a float value. In Python 2, the decimals are truncated.
• Annotations. Python 3 supports type annotations, whereas Python 2 does not. This feature can help with reading and
maintaining code.
• Print statement. Python 3 replaces the print statement with a print function.
• Syntax. Python 3’s syntax is considered easier to understand and more readable than Python 2’s, which uses more
complex syntax.
• Backward compatibility. Python 3 is not backward compatible with Python 2, whereas Python 2 is backward
compatible with previous versions of Python; in other words, code written for Python 1.x can still run on Python 2
without significant modifications.

08/03/2025
Install Python
Step 1
• Go to the official python download page for windows .
• Find a stable Python 3 release. This tutorial was tested with Python
version 3.11.8.
• Click the appropriate link for your system to download the executable
file: Windows installer (64-bit) or Windows installer (32-bit).

08/03/2025
Step 2
• After the installer is downloaded, double-click the .exe file, for
example python-3.11.8-amd64.exe, to run the Python
installer.
• Select the Install launcher for all users checkbox, which enables all
users of the computer to access the Python launcher application.
• Select the Add python.exe to PATH checkbox, which enables users to
launch Python from the command line.

08/03/2025
Step 2

08/03/2025
Step 3
• Add Python to environment variable .

• Pythonpath environment variable in Python allows you to include paths


to other Python files in your scripts and Python's import mechanism to
determine where to search for modules and files that use it. This can be
useful if you want to access different pieces of functionality from within
your script without having to type out the full path every time.

• https://www.digitalocean.com/community/tutorials/install-python-
windows-10
08/03/2025
Installation of Jupyter Notebook
Download Anaconda from Google.
Install it in System.
Select Jupyter notebook from options and launch.

08/03/2025
Python Syntax
• Indentation refers to the spaces at the beginning of a code line.
• Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
• Python uses indentation to indicate a block of code.
• Python is an interpreted programming language, this means that as a
developer you write Python (.py) files in a text editor and then put
those files into the python interpreter to be executed.
Python test.py

08/03/2025
Variables

• Variables are containers for storing data values.


x = 50
y = "Learntek"
print(x)
print(y)
Multiple variables can be used to store multiple values
simultaneously.

x,y,z =23,43,'ABC'
08/03/2025
Variable names
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume). Rules for Python variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
A variable name cannot be any of the Python keywords.
myvar = "Learntek"
my_var = "Learntek"
_my_var = "Learntek"
myVar = "Learntek"
MYVAR = "Learntek"
myvar2
08/03/2025
= "Learntek"
Global Variable
• Variables that are created outside of a function (as in all of the examples
above) are known as global variables.
• Global variables can be used by everyone, both inside of functions and
outside.

x = "awesome"

def myfunc():
print("Python is " + x)

myfunc()
08/03/2025
Python Datatypes
Text Type: str
Numeric int, float, complex
Types:

Sequence list, tuple, range


Types:

Mapping dict
Type:

Set Types: set, frozenset


Boolean bool
Type:

Binary bytes, bytearray, memoryview


Types:

None Type: NoneType


08/03/2025
Numeric Datatype
•X=3
• Y= 56.3
• Z = 23+65j

print(type(X))
print(type(y))
print(type(Z))

08/03/2025
Datatype casting
• X = int(3)
• Y= float(56)
• Z = 23+65j
• M= str('s1')

print(type(X))
print(type(y))
print(type(Z))

08/03/2025
String Datatype
• print("Hello")
print('Python 3.0')

To represent Multi string we use (''' ''')

a = """Lorem ipsum dolor sit amet,


consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""

print(a)
08/03/2025
Slicing String
• 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.
b = "Hello, World!"
print(b[2:5])
Slice from start of string
print(b[:5])
Get the characters from position 2, and all the way to the end.
Print(b[3:])
b = "Lets Learn Python!"
print(b[-5:-2])
08/03/2025
Interview questions
• How to split a string.
• How to extract last 3 elements from a string

08/03/2025
Methods to modify string

• a = " Hi, Let's learn python "


print(a.strip())
print(a.upper())
• a.lower()
• print(a.split(' , '))
• print(a.replace("Hi", "Hello"))
• a.startswith('Hi')
• a.endswith('python')

08/03/2025
Format String
• age = 30
txt = "My name is Jiten , My age is " + age
print(txt)

Else we can use :


txt = "My name is Jiten , My age is {} "
print( txt.format(age) )
print( f" My name is Jiten , My age is {txt} ")

08/03/2025
Python Operators
•=
• +=
• -=
•/
• //
•%
•*
• **

08/03/2025
List
• Lists are used to store multiple items in a single variable.
• Lists are created using square brackets.
• It is dynamic and mutable.
e.g
L1 = [23,34,54,'ABC']
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item
has
index [1] etc.
08/03/2025
Change List items
l2= [10, 40, "cherry",87, "Lassi", " ABCC", 64]
print(l2[-1])
Print(l2[2:5])
l2[-3:]
l2[1] = 70

08/03/2025
Methods of List
l = [20,32,94,21,48,34,43,14,3,452,3,64]
l.append(1000)
l.copy()--deep copy
l.clear()
l.extend()
l.insert(index_no, element)
l.pop()
l.remove()
l.reverse()
l.sort()

Interview questions:
Reverse a list without using inbuilt function?
08/03/2025
Deep copy vs Shallow copy
While copying the variable, we are making changes to one variable
which causes the same change in original variable,this is called as
Shallow copy.

If the change is not reflected in original variable, that is called and Deep
copy.

08/03/2025
List Comprehension
• List comprehension offers a shorter syntax when you want to create a
new list based on the values of an existing list.

• Based on a list of fruits, you want a new list, containing only the fruits
with the letter "a" in the name.
• Without list comprehension you will have to write a for statement
with a conditional test inside.
• e.g [i**2 for i in range(10)]

08/03/2025
Tuple
• Tuples are used to store multiple items in a single variable just like
LIST.
• Tuple is one of 4 built-in data types in Python used to store collections
of data.
• A tuple is a collection which is ordered and unchangeable.
• Tuples are written with round brackets.

08/03/2025
Unpack a Tuple
• in Python, we are also allowed to extract the values back into
variables. This is called "unpacking".

F= ("A", "B", "C")

(x,y,z) = F

print(x)
print(y)
print(z)

08/03/2025
Loop a tuple and Join tuples
• For I in tup:
• Print(I)

08/03/2025
Methods in Tuple
T = tuple()
T.index()
T.count()

08/03/2025
Difference Between List and
Tuple(interview question)
1.List is represented by [] where tuple is represented by ().
2.List is mutable whereas Tuple is Immutable.
3.Tuple is faster than list.
4.WHY tuple is faster compared to List.
5.Tuples are allocated large blocks of memory with lower overhead
than lists because they are immutable. whereas for lists, small memory
blocks are allocated. Thus, tuples tend to be faster than lists when
there are a large number of elements.

08/03/2025
Questions
T = (2,4,1,2,3,2,[6,78])
1. T[3] = 34, what would be the output T?
2. T[-1][-1] = ?
3. How to convert a tuple to list?
4. Write a program to find all the even elements from 1 to 100.
5. Write a program to find all the numbers divisible by 5 from 1 to 50.

08/03/2025
• How to join a list into string?
• How to extract only numbers and character from a list of
alphanumeric strings?
e.g [‘ABC56D’,’MN78’,’HJU’,’KL90’,’234’]

08/03/2025
Set
• Set is one kind of sequence datatype which is used to store multiple
elements.
• A set is a collection which is unordered, unchangeable*,
and unindexed.
• It is represented by curly braces.

08/03/2025
Properties of SET

Unordered
• Unordered means that the items in a set do not have a defined order.
• Set items can appear in a different order every time you use them, and
cannot be referred to by index or key.
Unchangeable
• Set items are unchangeable, meaning that we cannot change the items
after the set has been created.(we can add and remove elements but cant
change).
Duplicates Not Allowed
Sets cannot have two items with the same value.

08/03/2025
Methods of SET
Add()
Clear()
Copy()
Difference()
Symmetric_difference()
Intersection()
Pop()
Remove()
Union()
Update()
08/03/2025
Frozen set
• IT is similar to set but only differenece is we cant change it.
• It becomes similar to properties of tuple.

S = ['A', 23, 21, 'H']


Froz_s = frozenset(S)
Froz_s[1] = 34

08/03/2025
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.
• Represented by {} but in key:value format.
e.g dic = {'A': 123,'B':''MNP'}
D = dict(A=123, B='MNP')

We can access the dictionary by using key like


dic['A']
08/03/2025
Access dictionary elements
To access keys
Dic.keys()
To access values
Dic.values()

Get a perticular key in dictionary


Dic.get('key1')

08/03/2025
Add and remove elements
TO add elements to a dictionary we can use update method.
D = {'A' : 987 , 'B' : 783 , 'C' : 'ASJGDK'}
D['B'] = 387
D.update({'B' : 387})

TO remove elements from a dict.


D.pop('key1')
D.popItems()---it will remove the last inserted element.

08/03/2025
Dictionary comprehension
• IT is similar to list comprehension but the result will return a
dictionary.
{dic[j]=i for i,j in dic.items()}

{i for i in dic.keys()}

{j for j in dic.values()}

08/03/2025
What is ZIP and Enumerate
The enumerate() function returns indexes of all items in iterables (lists,
dictionaries, sets, etc.) whereas the zip() function is used to aggregate,
or combine, multiple iterables.

Zip(l1,l2)
Enumerate(l1)

08/03/2025
a=[1,2,3,4,5,6,7,8,9]
a[::2]=10,20,30,40,50
1.print(a)

What is output?
2. a=[1,2,3,4,5]
print(a[3:0:-1])

08/03/2025
3. A = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
Write a code to print 8.
4. a = (2)
print(type(a))
print(a*7)
5. b = (3,)
print(type(b))
print(a*3)

08/03/2025
6 . Print(list(range(10))

7. a = {(1,2):1,(2,3):2}

print(a[1,2])
8. my_dict = {}
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 4

Print(my_dict)
08/03/2025 ?
Any Questions?

08/03/2025

You might also like