Basic Python1
Basic Python1
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 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