Dsa (Week 1) - Python
Dsa (Week 1) - Python
Dsa (Week 1) - Python
Basics of Programming
Need Of Python Programming For DSA
• Being a High-Level and #1 Programming Language, using Python for Data Structure is
likely to be among the good choice due to its easy syntax and implementation.
• Python can be considered the best choice for DSA. Python is dynamically typed (no
need to declare types)and highly abstracted, it has a very clean and simplistic syntax. To
find a solution to any problem, you must first define the solution, analyze it and
implement the solution.
Python is easier to understand. Python offers simplicity in its syntax, which makes it
easier to learn. you don't have to write the data types in Python, as it is a dynamic
language. For writing a simple statement such as "Hello World" in Java, you will need at
least 3-4 lines of coding
Advantages of Python 3
1. Python 3 has a simple syntax that is easy to learn and read, making it a good choice
for beginners.
2. Python 3 is a high-level language that has a large standard library and many third-
party libraries available, making it a versatile language that can be used for a wide
variety of applications.
5. Python 3 has good support for data analysis and scientific computing, with libraries
such as NumPy and Pandas.
How to Get Started With Python?
• Python is a cross-platform programming language, which means that it can run on
multiple platforms like Windows, macOS, Linux,
• It is free and open-source.
• Install Python
• here's how you can install and run Python on your computer.
1.Download the latest version of Python.
2.Run the installer file and follow the steps to install Python
During the install process, check Add Python to environment variables. This will
add Python to environment variables, and you can run Python from any part of the
computer.
Once you finish the installation process, you can run
There are multiple ways to run Python on your System :
1. Using CMD
2. Using IDLE or other softwares (eg. VsCode, Pycharm, Jupyter Notebook)
Try typing in 1 + 1 and press enter. We get 2 as the output. This prompt can be used as a
calculator. To exit this mode, type quit() and press enter.
2. Run Python in the Integrated Development Environment (IDE)
We can use any text editing software to write a Python script file.
We just need to save it with the .py extension. But using an IDE can make our life a lot easier. IDE
is a piece of software that provides useful features like code hinting, syntax highlighting and
checking, file explorers, etc. to the programmer for application development.
Python Variables
• In other programming languages like C, C++, and Java, you will need to declare the
type of variables but in Python you don’t need to do that. Just type in the variable and
when values will be given to it, then it will automatically know whether the value given
would be an int, float, or char or even a String.
• Ex :-
myNumber=2
myNumber=True
myNumber=‘ak’
myNumber=5.6
Python Data Types
Python Data Types
1. Numbers Data Type
In Numbers, there are mainly 3 types which include Integer, Float, and Complex.
These 3 are defined as a class in Python. In order to find to which class the variable belongs to you can
use type () function.
Example:
a=5
b = 2.5
Note:-The type() method in Python can be used to determine variable’s data type. isinstance() determines
whether an object belongs to a specific class.
• float- holds floating precision numbers and it’s accurate up to 15 decimal places.
print(a)
print(b)
print(a,"concatenated with",b)
Example:
String1 = "Welcome"
print(String1+String2)
Example:
Print(String1*4)//Output: WelcomeWelcomeWelcomeWelcome
3. List Data Type
• A list can contain a series of values.
• List variables are declared by using brackets [ ]. A list is mutable, which means we can modify the list.
• list in Python is it can simultaneously hold different types of data.
Example:
List = [2,4,5.5,"Hi"]
List[3] = "Hello"
print(List)
3. List Data Type Cont…
#list of having only integers
a= [1,2,3,4,5,6]
print(a)
b=["hello","john","reese"]
print(b)
c= ["hey","you",1,2,3,"go"]
print(c)
Note:-As Tuples are immutable in Python, if we try to update the tuple, then it will generate an error.
Example:
Tuple[2]= "D"
print(a[1])
print(a[2])
print(a["age"])
6. Set Data Type
A set is an unordered collection of items. Set is defined by values separated by a comma
inside braces { }
Example:
Set = {5,1,2.6,"python"}
print(Set)
Example:
B = {'c', 'd', 2 }
print('A U B =', A| B)
A = {100, 7, 8}
B = {200, 4, 7}
print(A & B)
Output: {7}
Note:-As the set is an unordered collection, indexing has no meaning. Hence the slicing operator []
does not work.
Set[1] = 49.3
True is any non-zero number or the character 'T', while false is any non-zero value or the
character 'F‘.
data type conversion
• As the name says, it is converting the value of one data type to another. This process is
called data type conversion in Python.
How many types of conversion is there in Python?
• Similar to most languages, there are two types of conversion.
• Implicit Type Conversion
• Explicit Type Conversion
What is Implicit Type Conversion?
In implicit conversion, Python converts one data type into another data type without any
user involvement.
int_number = 843
float_number = 8.43
print("Result Value:",result)
int_number = 843
string_number = “843”
print("Data Type of Integer:",type(int_number ))
print("Data Type of String before
casting:",type(string_number ))
string_number = int(string_number )
print("Data Type of String after
casting:",type(string_number ))
result = int_number + string_number
print("Result:",result )
print("Data type of result :",type(result ))
What is Explicit Type Conversion?
• In Explicit conversion, the user has to convert the data type of an object to the desired
data type as per requirement. One can achieve the above using the default functions –
float(), int(), string().
• This process is sometimes called typecasting of data, as the user casts the data type of
any object.
• Consider the code below.
int_number = 843
string_number = “843”
print("Data Type of Integer:",type(int_number))
print("Data Type of String:",type(string_number))
print(int_number + string_number)
More About String Operations
• A string in Python can contain as many characters as you wish. The only limit is your
machine’s memory resources. A string can also be empty:
• What if you want to include a quote character as part of the string itself?
Because newlines can be included without escaping them, this also allows for
multiline strings:
Escape Sequences in Strings
Sometimes, you want Python to interpret a character or sequence of characters within a string
differently. This may occur in one of two ways:
•You may want to suppress the special interpretation that certain characters are usually given within a
string.
•You may want to apply special interpretation to characters in a string which would normally be taken
literally.
• To break up a string over more than one line, include a backslash before each newline,
and the newlines will be ignored:
• To include a literal backslash in a string, escape it with a backslash:
Manipulate Strings With Method
• Strings come bundled with special functions called string methods that you can use to
work with and manipulate strings
• The text you entered is repeated on a new line with single quotes. That’s because input()
returns as a string any text entered by the user.
• To see how input() works, type the following code into IDLE’s editor window
• The single space at the end of the string "Hey, what's up? " makes sure that when the
user starts to type, the text is separated from the prompt with a space. When the user
types a response and presses Enter , their response is assigned to the user_input variable
• Once you have input from a user, you can do something with it. For example, the
following program takes user input, converts it to uppercase with .upper(), and prints
the result:
• print("Welcome..." + user_response)
Operators and Expressions in python
Arithmetic Operators in Python
.These Python arithmetic operators include Python operators for basic mathematical
operations.
Note:-“The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left
hand operand by right-hand operand. It's used to get the remainder of a division problem.”
2. Python Relational Operator
3. Python Assignment Operator
4. Python Logical Operator
Special operators
Python language offers some special types of operators like the identity operator and the membership
operator.
Identity operators
In Python, is and is not are used to check if two values are located on the same part of the memory. Two
variables that are equal does not imply that they are identical.
In a dictionary we can only test for presence of key, not the value.
• Single-line comments begins with a hash(#) symbol and is useful in mentioning that the
whole line should be considered as a comment until the end of line.
• A Multi line comment is useful when we need to comment on many lines. In python,
triple double quote(“ “ “) and single quote(‘ ‘ ‘)are used for multi-line commenting.