Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4,6 TrustPilot
logo-home
Class notes

learn python language easily

Rating
-
Sold
1
Pages
22
Uploaded on
25-02-2023
Written in
2022/2023

providing all essential knowledge of python languages

Institution
Course

Content preview

Python

1. COMMON BUILT-IN DATA TYPES IN PYTHON:
There are several built-in data types in Python. Python provides type() and isinstance()
functions to check the type of these variables. These data types can be grouped into the
following categories-
None – None keyword represents the null keyword in Python.
Numeric – There are three distinct numeric types – integers, floating point numbers, complex
numbers. In addition Booleans are subtype of integers.
• int – Stores integer literals including hex, octal and binary numbers as integers. • float - Stores
literals containing decimal values and/or exponent sign as floating-point numbers. • complex –
Stores complex numbers in the form (A + Bj) and has attributes: real and imag. • bool – Stores
boolean value (True or False).
Sequence – According to Python there are four basic Sequence Types – lists, tuples, and
range.
• list – Lists are mutable sequences, typically used to store collections of homogeneous items. •
tuples – Tuples are immutable sequences, typically used to store collections of heterogeneous
data. • range – The range type represents an immutable sequence of numbers and is commonly
used for looping a specific number of times in for loops. • string – Immutable sequence of
characters to store textual data.
Mapping – Mapping objects are mutable and there is currently only one standard mapping type,
the dictionary.
• dict – Mutable unordered collection of items.
Set – Python has two built-in set types – set and frozenset.
• set – Mutable unordered collection of distinct hashable objects. • frozenset – Immutable
collection of distinct hashable objects.


2. CONTROL FLOW TOOLS IN PYTHON:
2.1 if statements
➢ The if statement is used in Python for decision making. ➢ It contains a body of
code which runs only when the condition given in the if statement is TRUE. ➢ If the
condition is FALSE, then the optional else statement runs which contains some code
for the else condition. ➢ The keyword ‘elif’ is short for ‘else if’ and is useful to avoid
excessive indentation.
2.2 for Statements
➢ It has an ability to iterate over the items of any sequence, such as a list or a
string, in order that they appear in the sequence. ➢ The for statement in Python
differs a bit from what you may be used to in C or Pascal.
2.3 The range () Function
➢ If you do need to iterate over a sequence of numbers, the built-in function range
() comes in handy. It generates arithmetic progressions. ➢ The given end point is
never part of the generated sequence. ➢ Example: range (5,10) Output: 5,6,7,8,9
2.4 Break and Continue statements

,Break Statement –
➢ The Break statement, like in C, breaks out of the innermost enclosing for or while
loop. ➢ The Break statement terminates the loop immediately, and control flows to
the statement after the body of the loop.
Continue Statement –
➢ The Continue statement, also borrowed from C, continues with the next iteration
of the loop. ➢ The Continue statement terminates the current iteration of the
statement, skips the rest of the code in the current iteration and control flows to the
next iteration of the loop.
2.5 Pass Statements


➢ The Pass statement does nothing. It can be used when the statement is required
syntactically but the program requires no action. ➢ Pass keyword in Python is
generally used to fill-up empty blocks and is similar to an empty statement
represented by semi-colon in languages such as Java, C++.
2.6 Defining Functions
The keyword def introduces a function definition. It must be followed by the function name and
the parenthesized list of formal parameters.
➢ The statements that form the body of the function start at the next line, and must
be intended.
Example: def fib(n):
#write Fibonacci series upto n
a,b=0,1
while a < n:
print(a, end=’ ‘)
a , b = b , a+b
print()
#Now call the function we just defined
fib(2000)
Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597

2.7 More on Defining Functions
2.7.1 Default Argument Values
➢ The most useful form is to specify a default value for one or more arguments. ➢
This creates a function that can be called with fewer arguments than it is defined to
allow. ➢ Example: def greet(name, msg=’Good morning!’):
print(“Hello”, name + ‘ , ‘ + msg)
greet (“Kate”)
greet(“Bruce”, ”How do you do!”)
Output: Hello Kate, Good morning!
Hello Bruce, How do you do?

2.7.2 Keyword Arguments
➢ Functions can also be called using keyword arguments of the form
kwarg = value.

, ➢ Example: def SomeFun (**kwargs):
for argumentname, argumentvalue in kwargs.items()
print(argumentname, argumentvalue)




SomeFun(name = ‘Kiran’ ,age = 23, role = ‘Engineer’)
Output: name = Kiran age = 23 role = Engineer
2.7.3 Special Parameters

➢ By default, arguments may be passed to a Python function either by position or
explicitly by keyword. ➢ For readability and performance, it makes sense to restrict
the way arguments can be passed so that a developer need only look at the
function definition tp determine if items are passed by position, by position or
keyword, or by keyword. • Positional – or – Keyword Arguments If / and * are not
present in the function definition, arguments may be passed to a function by
position or by keyword.
• Positional – Only Parameters It is possible to mark certain parameters as positional – only.
If positional – only, the parameters order matters, and the parameters cannot be passed by
keyword.
Positional – only parameters are placed before a / (forward slash).
• Keyword – Only Arguments To mark parameters as keyword – only, indicating the
parameters must be passed by keyword arguments, place an * in the arguments list just before
the first keyword – only parameters.
Example: def standard_arg (arg):
print(arg)
def pos_only_arg (arg, /):
print(arg)
def kwd_only_arg (*, arg)
print(arg)


2.7.4 Arbitrary Argument Lists
➢ The least frequency used open is to specify that a function can be called with an
arbitrary number of arguments. ➢ These arguments will be wrapped up in a tuple.
Before the variable number of arguments, zero or more normal arguments may
occur. ➢ Example: def concat (*args, sep = “ / “): return sep.join(args)
concat(“earth” , “mars” , “venus”) Output: ‘earth/mars/venus’
2.7.5 Unpacking Argument Lists
➢ The reverse situation occurs when the arguments are already in a list or tuple but
need to be unpacked for a function call requiring separate positional arguments. ➢
For instance, the built – in range() function expects separate start and stop
arguments. ➢ If they are not available separately, write the function call with the *
operator to unpack the arguments out of the list or tuple. ➢ Example:
list(range(3,6)) #normal call with separate arguments Output: [3,4,5]

Written for

Institution
Study
Course

Document information

Uploaded on
February 25, 2023
Number of pages
22
Written in
2022/2023
Type
Class notes
Professor(s)
Mahesh
Contains
All classes

Subjects

R185,48
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
bhagyeshbagde

Get to know the seller

Seller avatar
bhagyeshbagde GAMAKA AI
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
3 year
Number of followers
1
Documents
2
Last sold
3 year ago

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Trending documents

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions