python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

TELUSKO

#7 DICTIONARY

Dictionaries use key and value pairs

Numbers are keys and the names are values

We can use get to fetch the values:

if we want to merge the keys and values into a dictionary…

we use..

now if we want to add monika likes CS and delete harsh from the data…
Now if we want to create a new dictionary that has other dictionaries and lists within it..

{CONTINUE WITH #9}….

If different variables have the same data, then they are stored at the same address

To check the address of a variable ‘var’ : id(var)

Numeric datatypes: int, float, complex, Boolean

Different datatypes:
OPERATORS IN PYTHON

x=x+2 is same as writing x+=2

similarly, x=x*3 is same as writing x*=3

a=5, b=6 is same as writing a,b=5,6

relational operators:

now lets give a new value to a: a=6…

then checking if a and b are not equal…

logical operators: AND, OR, NOT

Converting decimal to binary

Converting binary to decimal

Swapping 2 variables… a=5, b=6 => a=6, b=5


M-1: creating a temporary variable

M-2: formula …but keep in mind that using formula here uses an extra bit (a=5, b=6 are both 2 bits
each but a+b is 11 which is 3 bits)

BITWISE NOT operator

why??->

BITWISE AND & BITWISE OR operators

why?? ->

XOR operator:

if its an odd no. of 1s then = 1. If its an even no. of 1s then = 0

Left shift: why 40?? …

10 in binary is 1010

i.e 1010.0000…

left shift this by 2

101000.0000…
This new binary no. is 40

RIGHT SHIFT:

10>>2 is 2. Why ??..

In right shift we shift the decimal point 2 digits left..

1010. becomes 10.10 becomes 10

Which when converted to decimal system is 2

To do math functions, we must type ‘import math’

We can also import math as another variable:

We can also import specific functions from math..

‘floor’ rounds off the decimal to the lower integer. ‘ceil’ rounds off the decimal to the higher integer. i.e

ceil 27.2 is 28 and floor 30.6 is 30

user input

Let us take a char input from user:

Now assume we want to use only a part of the char. We can achieve this by calling specific index values:

Or we can also do it like this:

Evaluating operations in an input:


If, else & elif:

While loop:

the 3 required steps in a while loop..

1. Initialisation

2. check the condition

3. increment/decrement

Code- result-

Code- result-

For loop:

Code- result-
Code- result-

Code- result-

Code-

for i in range(5):

print(i)

result-

Code- result-

Code for not printing multiples of 5 from 1 to 20-

(brackets for ‘if’ are optional)

BREAK, CONTINUE AND PASS:


Code-

result-

if we want all the no.s from 1 to 100 except multiples of 3 and 5…


if we want all even numbers from 1 to 100:

ARRAYS:

When creating arrays, we need to mention the type and the values:

We use typecodes to mention type…

use i for integers and u for characters

:unsigned integers mean that -ve values are not allowed:

:signed integers mean that -ve values are allowed:

--------------------------------------------------------------------------------------------------------------------------

Say we are creating and printing an integer array with the name ‘vorei’ then the code is:

From array import * {‘*’ means all}

vorei = array(‘i’, [value1,value2,..])

print(vorei)

--------------------------------------------------------------------------------------------------------------------------

print(vorei.buffer_info()) would have printed “(address of array, size of array)”

print(vorei.dtype) would have printed the type of values in the array

print(vorei.ndim) gives the no. of dimensions

print(vorei.shape) gives rows x columns

print(vorei.size) no. of elements in the n-dimensional array


print(vorei.

---------------------------------------------------------------------------------------------------------------------------

Code-

result-

to print a particular index we can type: print(vals[index])

& to print numbers one by one, we can use or we can use another variable and the name

of the array directly…

To make a new array with squares of elements from the old array, we can do:

This above code is using for loop.

If we wish to use while loop, then we have to follow

the 3 required steps in a while loop..

1. Initialisation

2. check the condition

3. increment/decrement

TO OPERATE A VIRTUAL ENV:


Type this in command prompt
1. virtualenv <name_of_virtualenv>……………………………………………….a directory with that name will be created
2. cd <name_of_virtualenv>…………………………………………………………..it will go to the specified directory
3. scripts\activate………………………………………………………………………….the virtual environment will get activated
4. …………………………………………………….it will look like this
5. Deactivate…………………………………………………………the virtual env will be deactivated
6. Cd .. ……………………………………………………………..it moves out of that directory
ON MY LAPTOP, VIRTUAL ENVIRONMENT IS INSTALLED IN BITS FOLDER->SWECHA INTERNSHIP->SWECHA

Our package of python does not support multidimensional arrays. i.e

returns error

Pass-by-value means that we are just passing a value

Pass-by-reference means that we are passing the address itself

Python does not have either of these

{ #33: function arguments in python….didnt understand squat }

Types of arguments in python:

1. Positional..
positions of variables in functions must be maintained
2. Keyword…
If positions are forgotten we can call&assign variables by keywords

3. Default…
If we don’t pass a value where it is required, then it should take a default value
Ex:

when no value is passed…code- result-

when a value is passed, default is overridden…code- result-


4. Variable length…
We can define a function where the number of arguments are not fixed
We can use * to denote that we are passing multiple arguments
Ex:

1. 2.

In these examples, *b is stored as a tuple. i.e print(b) gives us

5. Keyworded variable length…


We can define keyworded multiple arguments by using **
Ex:

code-

result-
…for this same result we can also implement for loop…

Code-

Result-

There is a default limit of 1000 in recursion

Print(sys.get recursionlimit()) to check limit of recursion


Print(sys.setrecursionlimit(2000)) to set recursion limit to 2000…….{may not work on very large no.s}
#41 done in playlist….shifted to full course one-shot at 3:15:41 https://youtu.be/YfO28Ihehbk?
si=3uQa8UL4ru2akHit&t=11741

‘pass’ is used to skip over a space where some code is mandatory but there is an absence of it

Types of variables:

Instance and class/static variables. In variables, class=static

Types of methods:

Instance, class & static methods. Class and static methods are different.

Copying and pasting images using file handling in python

Finished one-shot telusko video at 37. MySQL Workbench Setup - 06:24:05 … https://youtu.be/YfO28Ihehbk?si=-
kr5_2dPaqgI7tQA&t=23045

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy