python basics
python basics
Introduction to
Python with Data
Structure
Units for Discussion
Introduction to
Python Basics
Python Advance Data Structure
and Algorithms
using Python
Python Basics
DISCLAIMER
The content is curated from online/offline resources
and used for educational purpose only.
Learning Objectives
You'll learn in this unit
• Introduction to Python Programming Language
• Python IDE, different types of IDEs in Python
• Anaconda installation on windows/Linux
• Overview of Jupyter notebook
• Python variables
• Python built-in Data Types
• Operators in Python
• Different Data Structure
• Setting up Virtual Environment
Source :
www.freepik.com/
How does a computer program work?
• A program makes a computer usable. Without a program, a computer, even the most powerful one, is
nothing more than an object. Similarly, without a player, a piano is nothing more than a wooden box.
• Computers are able to perform very complex tasks, but this ability is not innate. A computer's nature is
quite different.
• It can execute only extremely simple operations. For example, a computer cannot understand the value
of a complicated mathematical function by itself, although this isn't beyond the realms of possibility in
the near future.
• Contemporary computers can only evaluate the results of very fundamental operations, like adding or
dividing, but they can do it very fast and can repeat these actions virtually any number of times.
Natural languages vs. programming languages
Structure Flexible, often ambiguous, relies on Rigid, strictly defined syntax and grammar
context and grammar
Usage Human communication Writing software, algorithms, and controlling
hardware
Complexity Highly complex with a vast vocabulary and Simpler, with a limited set of keywords and rules
rules
Error Handling Humans can often infer meaning from Requires explicit error handling and debugging
context and correct errors
Processing Processed by the human brain Processed by computers using compilers or
interpreters
Error High, humans can understand and adapt Low, syntax errors must be corrected for the
Tolerance to errors program to run
Machine language vs. high-level language
Source :
Reference
What is Python?
Interpretation vs Compilation
Interpretation:
• The process where the source code is
translated and executed line by line by an
interpreter at runtime, without producing a
separate machine code file.
• Examples include Python, JavaScript...
Compilation:
• The process where the entire source code is
translated into machine code before execution.
This machine code is typically stored in an
executable file.
• Examples include C, C++...
Source :
Reference
Key Differences
Translation Process:
• Compiled Languages: The compiler translates the whole program at once, creating an executable.
• Interpreted Languages: The interpreter translates and executes code line by line.
Execution Speed:
• Compiled Languages: Generally faster because the entire program is translated into machine code ahead of
time.
• Interpreted Languages: Slower due to line-by-line translation during execution.
Error Detection:
• Compiled Languages: Errors are detected at compile-time, before execution.
• Interpreted Languages: Errors are detected at runtime, potentially causing failures during execution.
Portability:
• Compiled Languages: Less portable because the compiled code is platform-specific.
• Interpreted Languages: More portable since the source code is interpreted by platform-specific interpreters.
Advantages Of Python
Source :
Reference
What Is IDE, Different Types Of Ides In Python
Source :
Reference
Python Setup – Getting Started With Programming
Lab - 1
Source :
Reference
Rules For Variable Names:
Python reserves 33 keywords in 3.3 versions for its use. Keywords are case sensitive in python. You can’t use a
keyword as variable name, function name or any other identifier name. Here is the list of keywords in python.
Source :
Reference
Comments In Python
Source :
Reference
Standard Data Types
Source :
Reference
Standard Data Types
Input() in Python
• Number data types store numeric values. Number • A complex number consists of an ordered pair of
objects are created when you assign a value to real floating-point numbers denoted by x + yj, where
them. x and y are the real numbers and j is the imaginary
unit.
Python Strings
• Strings in Python can be formatted with the use of format() method which is very versatile and powerful tool for
formatting of Strings.
• Format method in String contains curly braces {} as placeholders which can hold arguments according to
position or keyword to specify the order.
• A string can be left (<), right (>) or center(^) justified with the use of format specifiers, separated by colon (:).
Integers such as Binary, hexadecimal, etc. and floats can be rounded or displayed in the exponent form with
the use of format specifiers.
String formatting
Escape sequence:
\n – starts printing since the next line.
F-String
• The idea behind f-strings is to make string interpolation simpler, to create an f-string, prefix the string with the
letter “f ”.
• The string itself can be formatted in such the same way that you would with str.format(). F-strings provide a
concise and convenient way to embed python expressions inside string literals for formatting.
Example:
There are some basic string methods for various built-in data types:
methods Description
upper() Convert to upper case
lower() Convert to lower case
title() Convert the first letter of each word to caps
capitalize Convert first letter to caps
swapcase() Toggle case
isalpha() Return true if alphabet
isdigit() Return true if digit
isupper() Return true if upper case string
islower() Return true if string in title case
istitle() Returns true if lower case string
strip() Remove blank space from the both sides
split() Break the string into list of words
join() Join two string with given separator
Example:
Different Types Of Operators In Python
Example:
Assignment operator
Example: 1 Example: 2
Comparison operator
Example: 1
Example: 2
Logical Operator
Example:
Lab - 2
Perform Basic Operations of Python
Programming
Python List
• Split method:
Python List
• Concatenation:
Tuples
As we discussed tuple is not changeable that’s why we can't update anything in tuple
Python Dictionary
• Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl
and consist of key-value pairs.
• A dictionary key can be almost any Python type but are usually numbers or strings. Values, on the other
hand, can be any arbitrary Python object.
• Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square
braces ([]).
• For example − Create the blank dictionary named dict1 and dict2. Update the element in dict1 and dict2,
also print the keys and values from dict1 and dict2.
#Syntax of dictionary
dict = {“Keys” : “values”}
Python Dictionary
• For example −
Python Set
Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces {}.
• Unordered
• Unchangeable
• Not allowed duplicate values
• Example- For duplicates:
Type casting
Slicing is the process of accessing a sub-sequence of a sequence by specifying a starting and ending
index. In Python, you perform slicing using the colon : operator.
The syntax for slicing is as follows:
sequence[start_index:end_index]
where start_index is the index of the first element in the sub-sequence and end_index is the index of the
last element in the sub-sequence (excluding the element at the end_index). To slice a sequence, you can
use square brackets [] with the start and end indices separated by a colon.
For example:
my_list = ['apple', 'banana', 'cherry', 'date’]
print(my_list[1:3]) # output: ['banana', 'cherry']
Lab - 3
Benefits
• Isolation: Separate dependencies for each project.
• Consistency: Avoids version conflicts.
• Reproducibility: Easy to replicate environments.
Key Tools
• virtualenv: Create isolated Python environments.
• ipykernel: Use virtual environments as Jupyter kernels.
Lab - 4
• https://www.edureka.co/blog/variables-and-data-types-in-python/
• https://www.tutorialspoint.com/python/python_variable_types.htm
• https://data-flair.training/blogs/python-variables-and-data-types/
• https://www.programiz.com/python-programming/variables-datatypes
• https://www.tutorialspoint.com/numpy
• https://numpy.org/
• https://www.tutorialspoint.com/python_data_science
• https://www.geeksforgeeks.org/generating-random-number-list-in-python/
• https://www.w3schools.com/python/numpy/numpy_random.asp
• https://www.geeksforgeeks.org/numpy-asscalar-in-python/
• https://data-flair.training/blogs/numpy-statistical-functions/
• https://www.w3schools.in/python-tutorial/decision-making/
• https://www.geeksforgeeks.org/python-broadcasting-with-numpy-arrays/
• https://pythonexamples.org/python-type-casting/
Reference
• https://realpython.com/
• https://www.tutorialspoint.com/python/python_variable_types.htm
• https://www.tutorialspoint.com/python/python_variable_types.htm
• https://www.edureka.co/blog/variables-and-data-types-in-python/
• https://data-flair.training/blogs/python-variables-and-data-types/
• https://www.programiz.com/python-programming/variables-datatypes
• Jupyter Notebook: An Introduction – Real Python
• https://net-informations.com/python/iq/how.htm
• https://docs.python.org/3/faq/general.html#what-is-python
Let’s Start
Quiz
1. What will be the output of following code ?
i=1
while True:
if i%3 == 0:
break
print(i)
i+=1
a) 1 2 3
b) Error
c) 1 2
d) none of the mentioned
Answer: B
Error
Quiz
2. What is the method inside the class in python
language?
a) Object
b) Function
c) Attribute
d) Argument
Answer: B
Function
Quiz
3. Amongst which of the following is / are the
application areas of Python programming?
a) Web Development
b) Game Development
c) Artificial Intelligence and Machine Learning
d) All of the above
Answer: D
All of the above
Quiz
4. What are the values of the following Python
expressions?
2**(3**2)
(2**3)**2
2**3**2
Answer: A
512, 64, 512
Quiz
5. What will be the output of the following Python code?
Answer: C
[1, 2, ‘hello’]
Thank You