Python Strings and List
Python Strings and List
Strings in Python:
Python does not have a character data type, a single character is simply a string
with a length of 1. Square brackets can be used to access elements of the string.
LISTS:
Lists are one of the most powerful data structures in python. Lists are
sequenced data types. In Python, an empty list is created using list() function.
They are just like the arrays declared in other languages.
A single list can contain strings, integers, as well as other objects.
Lists can also be used for implementing stacks and queues.
Lists are mutable, i.e., they can be altered once declared. The elements
of list can be accessed using indexing and slicing operations.
In Python Lists are just like dynamically sized arrays, declared in other
languages (vector in C++ and ArrayList in Java).
In simple language, a list is a collection of things, enclosed in [ ] and
separated by commas.
The list is a sequence data type which is used to store the collection of
data.
Tuples and String are other types of sequence data types.
# creating empty list
l=[]
print(l)
l=[1,"one",2,"two",3,"three",4,"four"]
Print(l)
Ex:
t1=("apple","banana","orange")
print(t1[1:])
print(t1[0:1])
print(t1) output:
print(t1+t1)
print(t1*2)
print(type(t1))
Set:
Sets are used to store multiple items in a single variable. Set is one of 4 built-in
data types in Python used to store collections of data, the other 3 are List, Tuple, and
Dictionary, all with different qualities and usage.
•Note: Set items are unchangeable, but you can remove items and add new items.
Set Items:
Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered:
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and cannot be
referred to by index or key.
Unchangeable:
Set items are unchangeable, meaning that we cannot change the items after the set
has been created.Once a set is created, you cannot change its items, but you can
remove items and add new items.
Output:
{1, 2, 3, 4, 'bc', 'ac', 'a', ' ab'}
Method Description
clear() Removes all the elements from the dictionary
items() Returns a list containing a tuple for each key value pair
setdefault() Returns the value of the specified key. If the key does not exist:
insert the key, with the specified value
Dictionaries are written with curly brackets, and have keys and values:
Ex:
>>> digits={1:"one",2:"two",3:"three"}
>>> digits
Output: {1: 'one', 2: 'two', 3: 'three'}
Dictionary Items:
Dictionary items are ordered, changeable, and does not allow
duplicates.
Dictionary items are presented in key:value pairs, and can be referred to
by using the key name.
Ordered or Unordered:
Note:As of Python version 3.7, dictionaries are ordered. In Python 3.6 and
earlier, dictionaries are unordered.
When we say that dictionaries are ordered, it means that the items
have a defined order, and that order will not change.
Unordered means that the items does not have a defined order, you
cannot refer to an item by using an index.
Changeable:
Dictionaries are changeable, meaning that we can change, add or
remove items after the dictionary has been created.
print(car)
Python Functions:
A Python function is a reusable, organized block of code that is used to
perform the specific task. The functions are the appropriate way to divide an extensive
program into a useful block. It also provides reusability to our program. A code block
can be reused by calling the function.
All functions are treated as an object in Python, so it is more flexible than other
programming languages.
In Python, there are two types of functions:
Built-in functions – These functions are the part of the Python libraries and packages.
These are also called as pre-defined functions.
User-defined functions – These functions are defined by the user as per their
requirement.
Syntax:
C syntax:
return_type function_name ( paramete
{
body of the function
}
Creating a function:
Below are the basic steps to create a user-defined function.
Arguments should be written inside the opening and closing parentheses of the
function, and end the declaration with a colon.
The return statement is optional. It should be written at the end of the function.
Creating a Python Function:
We can create a Python function using the def keyword.
Calling a Function
After declaring a function, it must be called using function name followed
by parentheses with appropriate argument.
Note: It is necessary to define a function before calling; otherwise, it will give an error.
Example 1:
def disp():
print(“hello world”)
disp()
2. Default argument:
The default arguments are those arguments that assign a value with the
argument at the time of function definition. If the argument is not specified at the
time of function call, then it will be initialized with the value which was given in the
definition. For example
def add(a,b=30): #default value def disp(name,age=45):
c=a+b print('My name is:',name)
return c print('My age is',age)
sum=add(10) disp("raju")
Print(sum); disp("srinivas",35)
3.Keyword arguments:
The benefit of keyword arguments is that we can pass the argument in the
random order, which means the order of passing arguments doesn’t matter. Each
argument treated as the keyword. It will match argument names in the function
definition and function call.
def employee(id,name,age):
employee(age=30,id=1,name= 'Srinivas‘)
employee(id=1,age=30,name='RAJU')
employee(name='RAVI',age=30,id=1)
4. Variable-length arguments:
Some times we are not sure about numbers of argument that can be passed
to a function, for such scenario, we use variable-length arguments.
There are two types of variable-length argument in a function:
We should use an asterisk ( * ) before the argument name to pass variable length
arguments. The arguments are passed as a tuple, and these passed arguments make
tuple inside the function with the same name as the argument excluding asterisk *.
def variable(*names):
for I in names:
print(I )
variable(‘ raju ', ‘ ravi', ‘ kishan', ' devid', 'mohan')
**kwarg arguments
We cannot pass the keyword argument using *args. Python provides
**kwargs; It allows us to pass variable-length of keyword argument to the function.
We must use the double-asterisk ** before the argument name to denote
this type of argument. The arguments are passed as a dictionary, and these
arguments make a dictionary inside the function with name same as the parameter
without double asterisk **.
For example:
def variable(**names):
for key, value in names . items():
print(key , value)
variable( first_ name = “raju ", last _ name=' ravi ', Age=25,Salary=34000)