Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
83 views

Basic Python1

This document provides summaries of key Python concepts including: 1. Common data types in Python include lists, tuples, dictionaries, strings, and numeric types. 2. Functions allow code reuse and organization by defining blocks of code that can be executed multiple times by calling the function. 3. Loops like for and while loops are used to repeatedly execute blocks of code while a condition is true or until a sequence is traversed. 4. List comprehensions provide a concise way to create new lists from existing lists by applying operations to each element.

Uploaded by

Prafull Sutar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Basic Python1

This document provides summaries of key Python concepts including: 1. Common data types in Python include lists, tuples, dictionaries, strings, and numeric types. 2. Functions allow code reuse and organization by defining blocks of code that can be executed multiple times by calling the function. 3. Loops like for and while loops are used to repeatedly execute blocks of code while a condition is true or until a sequence is traversed. 4. List comprehensions provide a concise way to create new lists from existing lists by applying operations to each element.

Uploaded by

Prafull Sutar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 163

t is a list of all keywords in python programming.

I
7. What are the common built-in data types in Python?
Back to list
In Python, the sequence data type refers to a collection of elements or items
that are ordered and accessible by their position or index. Sequences are an
essential part of Python, and they allow you to store and manipulate a group
of related elements as a single entity.
Fromkeys
the isalnum() method is a built-in string method used to
check if all the characters in a given string are alphanumeric.
RANGE
The range() Function
With the help of the range() function, we may produce a series of
numbers. range(10) will produce values between 0 and 9. (10
numbers).
We can give specific start, stop, and step size values in the manner
range(start, stop, step size). If the step size is not specified, it defaults
to 1.
Since it doesn't create every value it "contains" after we construct it,
the range object can be characterized as being "slow." It does provide
in, len, and __getitem__ actions, but it is not an iterator.
The example that follows will make this clear.
Code
1. # Python program to show the working of range() function
2.
3. print(range(15)) Need to mention in list(range())
4.
5. print(list(range(15)))
6.
7. print(list(range(4, 9)))
8.
9. print(list(range(5, 25, 4)))
Output:
range(0, 15)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[4, 5, 6, 7, 8]
[5, 9, 13, 17, 21]
To iterate through a sequence of items, we can apply the range()
method in for loops. We can use indexing to iterate through the
given sequence by combining it with an iterable's len() function.
Here's an illustration.
Code
1. # Python program to iterate over a sequence with the help of ind
exing
2.
3. tup= ("Python", "Loops", "Sequence", "Condition", "Range")
4.
5. # iterating over tuple_ using range() function
6. for iterator in range(len(tuple_)):
7. print(tup[iterator].upper())
Output:
PYTHON
LOOPS
SEQUENCE
CONDITION
RANGE

The for Loop


Python's for loop is designed to repeatedly execute a code block
while iterating through a list, tuple, dictionary, or other iterable
objects of Python. The process of traversing a sequence is known as
iteration.
Syntax of the for Loop
1. for value in sequence:
2. { code block }
In this case, the variable value is used to hold the value of every item
present in the sequence before the iteration begins until this
particular iteration is completed.
Loop iterates until the final item of the sequence are reached.
Code
1. # Python program to show how the for loop works
# Creating a sequence which is a tuple of numbers
2. numbers = [4, 2, 6, 7, 3, 5, 8, 10, 6, 1, 9, 2]
3.
4. # variable to store the square of the number
5. square = 0
6.
7. # Creating an empty list
8. squares = []
9. # Creating a for loop
10. for value in numbers:
11. square = value ** 2
12. squares.append(square)
13. print("The list of squares is", squares)
Output:

The list of squares is [16, 4, 36, 49, 9, 25, 64, 100, 36, 1, 81,
4]
Using else Statement with for Loop
As already said, a for loop executes the code block until the sequence
element is reached. The statement is written right after the for loop
is executed after the execution of the for loop is complete.
Only if the execution is complete does the else statement comes into
play. It won't be executed if we exit the loop or if an error is thrown.
Here is a code to better understand if-else statements.
Code
While Loop
While loops are used in Python to iterate until a specified condition is
met. However, the statement in the program that follows the while
loop is executed once the condition changes to false.
What are Python Functions?
A function is a collection of related assertions that performs a
mathematical, analytical, or evaluative operation.
A collection of statements called Python Functions returns the
particular task.
Advantages of Functions in Python
Python functions have the following Perks.
o By including functions, we can prevent repeating the same code
block repeatedly in a program.
o Python functions, once defined, can be called many times and
from anywhere in a program.
o If our Python program is large, it can be separated into
numerous functions which is simple to track.
o The key accomplishment of Python functions is we can return as
many outputs as we want with different arguments.
The following elements make up to define a function, as seen above.
o The beginning of a function header is indicated by a keyword
called def.
o function_name is the function's name that we can use to
separate it from others. We will use this name to call the
function later in the program. In Python, name functions must
follow the same rules as naming variables.
o We pass arguments to the defined function using parameters.
However, they are optional.
o The function header is terminated by a colon (:).
o We can use a documentation string called docstring in the short
form to explain the purpose of the function.
o The body of the function is made up of several valid Python
statements. The indentation depth of the whole code block must
be the same (usually 4 spaces).
o We can use a return expression to return a value from a defined
function.
2.DEFAULT ARGUMENT:-
List comprehensions are often favoured over traditional loops when you want to
create a new list from an existing one or perform some operation on each
element. They make your code more concise and easier to understand.
However, for very complex or nested operations, it's essential to balance
readability with brevity, and in some cases, a regular loop might be more
appropriate.
Anonymous-निनावी(unnamed)
Greatest no. between 3 number
DECORATOR - Is function which takes function as argument

Step – 1 Aliasing
Step – 2 Calling Inner Function

You might also like