0% found this document useful (0 votes)
5 views8 pages

Python Variables and Functions Guide

Uploaded by

Prabhav Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Python Variables and Functions Guide

Uploaded by

Prabhav Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

python

no syntax for variables

just write

x,y,z = 20,30, 40

identifiers - name used to identify a variable

no special character except _ can be used in it

dont use keyword

case sensitive

string literal = " " or ' '

multiple line = '''write ur text '''

no fraction - int

long number followed by L pr l = long

float - both integer and fraction part

complext - a +bj form


python has only 1 special literal - that is none

type() - type of data

/ will always return a float type result

// - returns quotient

% - returns remainder

5 ** 3 - exponential

== for comparing

If false:

it will not go inside the statement

------

[] - list

[Link]("xyz")

for item in list

print(item)
list[0] - first item in list

for i in range (5) - [0 to 5)

i+=1 - incrementing i by 1

i-= 1 - decrementing i by 1

else absolutely works if none of the conditions work, there is no condition in


else

true and true - gives true

other cases false

false or false - false

other cases - true

--------

Functions

def ourprint(s):

print(s + " hello")


ourprint("hello world")

print("2"+"3")

print("hello"+ "world")

type(s) - will show error as s doesn't exist outside the function

if functn doesn't return anything - it is a none function

printing something - effects the outside environment -side effect

important to know if a fn has a side effect or not

----

a = [1,2,3]

b = a -- b is also referring the same array [123]

a ==b

true

a is b

true

a == [1,2,3]
true

**** a is [1,2,3]

false

because a is 1 array and [1,2,3,4] is another array

how do we get a copy

from copy import deepcopy

b = deepcopy(a)

in case of variables assigned to a single number

q=5

w=q

q =7

print q

print w

7
5,

this time it doesn't work like the list

so in case of list

we didn't change our mind about the list , just changed the part of the list, it
is the original one only

but in single number

q was assigned a new value completely

w stayed the same as 5

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

def add(a,b, c =4)

so if u dont give any value for c, it will take the default value as 4

otherwise it will take ur given value

Also,

def add (a=4,b)

add(2)
will throw error, required one should be before the optional one

!ls - to check the files we have

with open(filename, r) as f:

print([Link]())

returns list

([Link]()) - string on the file

Arrays

A = [1,2,4,6,7]

5 in A - checks if 5 is present in the array or not

DYNAMIC ARRAYS - ALSO CALLED LIST

sometimes adding new elements in dynamic arrays O(1) while sometimes it


can be O(n)

Python doubles in size everytime u fill the static array made for dynamic
array

You might also like