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

4

The document discusses various Python data types, operators, control flow statements and functions. It describes int, float, complex, strings, lists, tuples, sets, dictionaries, functions, recursion, loops including while, for loops, if/else statements and more.

Uploaded by

223111
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)
21 views

4

The document discusses various Python data types, operators, control flow statements and functions. It describes int, float, complex, strings, lists, tuples, sets, dictionaries, functions, recursion, loops including while, for loops, if/else statements and more.

Uploaded by

223111
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/ 20

Data types:

int - holds signed integers of non-limited length.

float - holds floating decimal points and it's accurate up to 15 decimal places.

complex - holds complex numbers.

Str-string (sentences or words).

Number Systems:
Python Keywords:

Python Data Types:


Arithmetic Operators:

Comparison operators:

Logical operators:
Bitwise operators:

Create a List:

We create a list by placing elements inside [], separated by commas.

Ex:

List = [1,2,3,5]

*A list can store elements of different types (integer, float, string, etc.), store duplicate elements

List Methods:
Python Tuple:

A tuple in Python is similar to a list. The difference between the two is that we cannot change the
elements of a tuple once it is assigned whereas we can change the elements of a list.

A tuple is created by placing all the items (elements) inside parentheses (), separated by commas.
The parentheses are optional; however, it is a good practice to use them.

We use the index number to access tuple elements, same as a list

We use the in keyword to check if an item exists in the tuple or not.

We generally use tuples for heterogeneous (different) data types and lists for homogeneous (similar)
data types. Since tuples are immutable, iterating through a tuple is faster than with a list. So, there is
a slight performance boost. Tuples that contain immutable elements can be used as a key for a
dictionary. With lists, this is not possible. If you have data that doesn't change, implementing it as
tuple will guarantee that it remains write-protected.

Python Strings:

In computer programming, a string is a sequence of characters. For example, "hello" is a string


containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.

We use single quotes or double quotes to represent a string in Python.

Methods of Python String:


Python Sets:

A set is a collection of unique data. That is, elements of a set cannot be duplicate.

In Python, we create sets by placing all the elements inside curly braces {}, separated by comma.

A set can have any number of items and they may be of different types (integer, float, tuple, string
etc.).

But a set cannot have mutable elements like lists, sets or dictionaries as its elements.

* When you run a code, you might get output in a different order. This is because the set has no
particular order.

Functions with Set:


Python Dictionary:

In Python, a dictionary is a collection that allows us to store data in key-value pairs.

We create dictionaries by placing key:value pairs inside curly brackets {}, separated by commas.

*Dictionary keys must be immutable, such as tuples, strings, integers, etc. We cannot use mutable
(changeable) objects such as lists as keys.

Python Dictionary Methods:

Python Functions:

A function is a block of code that performs a specific task.

Standard library functions - These are built-in functions in Python that are available to use.

User-defined functions - We can create our own functions based on our requirements.

Components used for writing functions: The def keyword, along with the function name is used to
define the function.

The identifier rule must follow the function name.

A function accepts the parameter (argument), and they can be optional.

The function block is started with the colon (:), and block statements must be at the same
indentation.

The return statement is used to return the value. A function can have only one return.
When the function is called, the control of the program goes to the function definition.

All codes inside the function are executed.

The control of the program jumps to the next statement after the function call.

After creating a function we can call it by using the name of the function followed by parenthesis
containing parameters of that particular function.

The return Statement in Python:

A Python function may or may not return a value. If we want our function to return some value to a
function call, we use the return statement.

The return statement also denotes that the function has ended. Any code after return is not
executed.

Python Recursion:

Recursion is the process of defining something in terms of itself.

In Python, we know that a function can call other functions. It is even possible for the function to call
itself. These types of construct are termed as recursive functions.

Loops:

While Loops:

A while loop statement in Python programming language repeatedly executes a target statement as
long as a given condition is true.

The syntax of a while loop in Python programming language is,


initialising

while (expression):

statement

increment or decrement

The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the
loop.

Infinite loop:

A loop becomes infinite loop if a condition never becomes FALSE.

You must be cautious when using while loops because of the possibility that this condition never
resolves to a FALSE value.

For loop:

For statement in Python has the ability to iterate over the items of any sequence, such as a list or a
string.

for iterating_var in sequence:

statements(s)

*No need of increment and initialising

Range:

The range() function

list(range(5))--------- [0, 1, 2, 3, 4]

range(5) -------------range(0, 5)

Else in for loop:

If the else statement is used with a for loop, the else block is executed only if for loops terminates
normally (and not by encountering break statement).

If the else statement is used with a while loop, the else statement is executed when the condition
becomes false.
Nested loops:

Python programming language allows the use of one loop inside another loop.

This is called Nested loop

Ex*

for iterating_var in sequence:

for iterating_var in sequence:

statements(s)

statements(s)

Loop control statements:

Return statement:

The function return statement is used to exit from a function and go back to the function caller and
return the specified value or data item to the caller.

The return statement can consist of a variable, an expression, or a constant which is returned to the
end of the function execution. If none of the above is present with the return statement a None
object is returned.

If Statement:

if statement is the most simple decision-making statement. It is used to decide whether a certain
statement or block of statements will be executed or not
If – else:

The if statement alone tells us that if a condition is true it will execute a block of statements and if
the condition is false it won’t. But what if we want to do something else if the condition is false. Here
comes the else statement. We can use the else statement with if statement to execute a block of
code when the condition is false.

Nested If:

A nested if is an if statement that is the target of another if statement. Nested if statements mean an
if statement inside another if statement. Yes, Python allows us to nest if statements within if
statements.

if-elif-else ladder :

Here, a user can decide among multiple options. The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else
statement will be executed
Code:

def linearSearch(list1, n, x):

# Going through array sequentially

for i in range(0, n):

if (list1[i] == x):

return i

return -1

l = [2, 4, 0, 1, 9]

x=1

n = len(l)

result = linearSearch(l, n, x)

if(result == -1):

print("Element not found")

else:

print("Element found at index: ", result)


Code:

def binarySearch(list1, x, low, high):

while low <= high:

mid = low + (high - low)//2

if list1[mid] == x:

return mid

elif list1[mid] < x:

low = mid + 1

else:

high = mid - 1

return -1

l = [3, 4, 5, 6, 7, 8, 9]

Low=0
High=len(l)-1

x=4

result = binarySearch(l, x, Low,High)

if result != -1:

print("Element is present at index " + str(result))

else:

print("Not found")

You might also like