Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
College aantekeningen

learn python language easily

Beoordeling
-
Verkocht
1
Pagina's
22
Geüpload op
25-02-2023
Geschreven in
2022/2023

providing all essential knowledge of python languages

Instelling
Vak

Voorbeeld van de inhoud

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]

Geschreven voor

Instelling
Studie
Vak

Documentinformatie

Geüpload op
25 februari 2023
Aantal pagina's
22
Geschreven in
2022/2023
Type
College aantekeningen
Docent(en)
Mahesh
Bevat
Alle colleges

Onderwerpen

€9,42
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kan je een ander document kiezen. Je kan het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
bhagyeshbagde

Maak kennis met de verkoper

Seller avatar
bhagyeshbagde GAMAKA AI
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
1
Lid sinds
3 jaar
Aantal volgers
1
Documenten
2
Laatst verkocht
3 jaar geleden

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Populaire documenten

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Veelgestelde vragen