O GoConqr está de visual novo! Conte para nós o que você achou.

Notas

python

Yogitha Pikkli Python midterm Inglês 5 páginas

Conteúdo das notas (5)

Página 1

A program is a list of instructions or statements under which a computer performs or executes a task in the order in which the inst. sets appear.

x = 1 : assignment statement

variable name : can be as long as you want can include alphabets, numbers and underscore but should not start with number, space, or any other symbol.

Case sensitive : x and X are two diff variables.

Comments : # : anything that starts with a # is ignored by the computer. can be inserted anywhere.

Variable types : integers (both positive and negative)

float(can take integer values as well)

complex(takes j as the imaginary number and not i)

string (" " anything in between the quotes is considered as a string)

Output and input statements : print(x) : prints the value of the variable x.

print(x, z, sep="...") : prints the value with a separator(three dots in this case) in between x and z.

x= input(x) : input statement

x,y = 3,2 : x will be 3 and y will be 2

Arithmetic Operations : x+y - addition

x-y - subtraction

x*y - multiplication

x/y - division

x%y - modulus

x**y - x raised to the power of y

x//y - rounded to the nearest integer

Packages : collections of useful related things

Modules : subpackages

Math package : log, log10, exp, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, sqrt also contains gamma functions, e and pi.

Built-in-functions : already present functions like float, complex, print

Página 2

Controlling with IF and WHILE:

If is indented with else blocks, an use multiple if and elif and else statements in one code.

LISTS and ARRAYS: types of containers to store values

LISTS:

LIST: elements can be of diff types, r = [5,6,9], list starts from 0, negative indexing - from the end of the list.

r=sum(list) : can sum the list

max : max value in list

min : ,in val in list

len : length of list

map : meta function which allows you to apply functions like sqrt and log to all the values in a list

append : appends an element at the end

pop : remove elements from a list

lists are mutable,dynamic and ordered.

singleton list : list with a single object

slicing : a[m:n]: print only from m till n-1. m,n can be negative as well.

Omitting the first index starts the slice at the beginning of the list, and omitting the second index extends the slice to the end of the list

if a is a list, a[:] returns a new object that is a copy of a

The syntax for reversing a list works the same way it does for strings : a[::-1]

+ : concatination of two lists

* : replication of lists

del : delete an element in a list

.extend, .remove, .insert,

TUPLES:

Tuples are defined by enclosing the elements in parentheses (()) instead of square brackets ([]).

Tuples are immutable

indexing, slicing, list reversal and string is same as lists

Cannot be modified

Advantages : execution faster, use this when data is going to remain unchanged

Singleton : [2,] : specify the comma for it to understand that this is a tuple and not an integer or an object.

packing and unpacking : When this occurs, it is as though the items in the tuple have been “packed” into the object. If that “packed” object is subsequently assigned to a new tuple, the individual items are “unpacked” into the objects in the tuple

Página 3

Slicing:

works for both arrays as well as lists

if r is a list then r[2:5] is a subset of the list r containing elements from 2 to 5

elements are from m to n-1 as the indexing starts from 0

r[2:] : all elements of the list from 2 till the end of the list

r[:5] : all the elements of the list from the beginning till 5 (not including 5)

r[:] : all elements from the beginning to the end of the list

Slicing works with two dimensional arrays as well

Dictionaries :

Dictionaries and lists share the following characteristics:

Both are mutable.

Both are dynamic. They can grow and shrink as needed.

Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa.

Dictionary is accessed via keys.

Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.

({}) - defining a dictionary, colon separates the keys and values

dict(), dict(' ' ,' '), dict(key='value', key2 =' value2')

some functions:

d.clear

d.get

d.items

d.keys

d.values

d.pop(key,default)

d.popitem

d.update

Página 4

ARRAYS:

No of elements is fixed and the type of the elements in an array should be the same.

arrays can be two dimensional, they behave like vectors or matrices, arrays work faster than lists

use the numpy package : numerical python

a=zeros(n,dtype)

a=ones(n,dtype)

a=zeros([m,n],dtype)

a=empty(,dtype)

a=array(list,dtype) : to convert a list into an array

numbering of elements start from 0

reading array from file : from numpy import loadtxt

a=loadtxt("filename.txt",dtype)

print(a) : prints out the file in an array format

we can perform airthmetic functions either to the individual elements or with the whole array itself

for adding or subtracting two arrays it is important that both of them are of the same size.

to get the dot product of arrays : from numpy import dot,array

a=array()

b=array()

d=dot(a,b)

print(d)

max,min,sum and map work similar to lists

instead of len we have shape and size for ndim array. size: size of the array (total no of elements in all rows and columns); shape: shape of the array (gives dimension of the array along each axis in the form of a list

to copy an array you have to import : from numpy import copy

Página 5

Regular Expressions :

help us to find if a pattern is present in a given string or sequence of characters or data.

manipulate textual data - useful for data mining

supported by the re module.

re.match(pattern,sequence) : matches a given pattern

re.split('+\s', text) : splits the texts with one or more ( because of +) white spaces (s).

re.findall(text) : finds the given text

re.search() :

Informações

Descrição
midterm Python Notas sobre python, criado por Yogitha Pikkli em 04-10-2019.
Criado por
Yogitha Pikkli
Visualizações
3
Avaliação

Fale Conosco

Sobre o que é sua mensagem?

O que você acha do novo visual?

Você gostou?