Python for Coding Interviews - Everything you need to Know
Summary
TLDRIn this coding tutorial, the host shares insights on using Python for coding interviews, highlighting its simplicity and conciseness. They discuss Python's dynamic typing, control structures, and various data structures like lists, sets, and dictionaries, emphasizing their practical use in interviews. The video also covers string manipulation, advanced data structures like heaps, and Python's syntax for functions and classes. Aimed at interview preparation, the host recommends neco.io for further resources, courses, and practice problems in multiple programming languages.
Takeaways
- 😀 The speaker learned Python specifically for coding interviews and found it helpful for its conciseness.
- 🔍 Python is a dynamically typed language, which allows for flexibility in variable assignment without declaring types.
- 🚫 Python does not support the '++' operator for incrementing variables, differing from some other programming languages.
- 💡 The 'None' keyword in Python represents null, and variables can be reassigned from other types to 'None'.
- 🔑 Python uses indentation rather than curly braces to define code blocks, which is a syntactical difference from languages like Java or C++.
- 🔄 The 'and' and 'or' keywords are used for logical operations instead of symbols, enhancing readability.
- 🔢 Python performs decimal division by default, unlike many languages that default to integer division.
- 📚 Lists (arrays) in Python are dynamic and provide various methods for manipulation, including append, pop, insert, and slice.
- 🔤 Strings in Python are immutable, meaning they cannot be changed after creation, but new strings can be created easily.
- 🔄 The 'zip' function is useful for iterating over multiple arrays simultaneously, combining their elements into pairs.
- 📚 Dictionaries (hashmaps) in Python are versatile and allow for key-value pair storage with efficient search and insertion.
Q & A
What is the main focus of the video?
-The main focus of the video is to cover everything one needs to know about Python for coding interviews.
Why did the channel creator learn Python specifically for coding interviews?
-The channel creator learned Python specifically for coding interviews because it is easier and more concise than most languages, which was helpful throughout all his interviews at Google.
What are some benefits of Python mentioned in the video?
-Python is mentioned to be beneficial for coding interviews due to its ease of use, conciseness, and dynamic typing, which allows for flexibility in variable assignments and types.
How does Python handle variable types differently from statically typed languages?
-Python, being a dynamically typed language, allows variables to be assigned values without declaring their types, and types are determined at runtime, enabling variables to change types.
What is the significance of the 'None' keyword in Python?
-In Python, 'None' represents the absence of a value, which is similar to 'null' in other languages, and it can be assigned to variables that are expected to hold other types of values.
How does Python syntax for if statements differ from other languages?
-Python syntax for if statements does not require parentheses for the conditional and uses indentation instead of curly braces to define the block of code associated with the conditional.
What is the default behavior of division in Python?
-In Python, division is decimal division by default, which means it will produce a floating-point result, unlike many other languages that use integer division by default.
How can one perform integer division in Python?
-To perform integer division in Python, one can use the double slash operator '//', which will round the result towards zero.
What is a key difference between Python lists and arrays in other languages?
-Python lists are dynamic arrays that can have items of different types and are easily manipulated with various built-in methods, such as append, pop, and sort.
How does Python handle string immutability?
-Python strings are immutable, which means that once a string is created, its characters cannot be changed. Any operation that appears to modify a string actually creates a new string.
What is a unique feature of Python sets?
-Python sets are collections of unique elements and are useful for membership testing, removing duplicates, and performing mathematical set operations.
How can one implement a Max Heap in Python?
-In Python, one can implement a Max Heap by using the negative values of the elements, pushing them onto a Min Heap, and then negating them again when popping from the heap.
Outlines
🐍 Introduction to Python for Coding Interviews
The speaker begins by welcoming the audience to a coding tutorial focused on Python for coding interviews. They share their personal journey of starting the channel during the pandemic, solving coding interview questions in Python, and eventually securing a job at Google. The speaker emphasizes Python's simplicity and conciseness, which they believe is advantageous for coding interviews despite never using it professionally. They encourage viewers, whether new to Python or experienced with other languages like Java or C++, to appreciate Python's benefits. The speaker also promotes their website, neco.io, which offers free resources for coding interview preparation and mentions their advanced algorithms course and upcoming system design course.
🔍 Deep Dive into Python's Dynamic Typing and Syntax
This section delves into the dynamic typing nature of Python, where variables like 'n' can be reassigned from an integer to a string without type declaration. The speaker explains Python's syntax peculiarities, such as the lack of parentheses in conditional statements and the use of indentation to define code blocks. They discuss Python's null value 'None', the absence of '++' operator, and the use of 'and' and 'or' for logical operations. The paragraph also covers Python's for and while loops, explaining the use of 'range' and how loops are structured differently from languages like Java or C++.
🔢 Exploring Python's Numerical Operations and Data Structures
The speaker explores Python's handling of numerical operations, highlighting the default decimal division and the use of '//' for integer division. They discuss Python's approach to modulo operations with negative numbers and provide alternatives for achieving results consistent with other languages. The section continues with an overview of Python's lists, their dynamic nature, and operations like appending, popping, and inserting elements. The speaker also introduces list comprehensions and demonstrates how to create lists and multi-dimensional arrays efficiently.
📚 Advanced Python Features: Slicing, Unpacking, and More
This part of the script focuses on advanced Python list manipulation techniques like slicing and unpacking. The speaker explains how to extract sublists and assign multiple list elements to variables. They also cover iterating through lists using various methods, including simple for loops, iterating directly through values, and using 'enumerate' for index and value pairs. The paragraph extends to discussing list reversal and sorting, both in ascending and descending orders, and custom sorting using lambda functions. The speaker also touches on initializing lists using list comprehensions for more complex scenarios.
💬 Strings, Queues, and Sets in Python
The speaker transitions to discussing Python strings, their immutability, and operations like slicing and conversion between strings and integers. They introduce Python's deque for double-ended queues, explaining how to append and pop from both ends efficiently. The paragraph continues with a discussion on sets, their properties, and operations like insertion, search, and removal. The speaker also covers set comprehensions and the use of tuples as immutable sequences and hashable keys for sets and dictionaries.
🏢 Python Data Structures: Dictionaries, Heaps, and Functions
This section covers Python dictionaries, their initialization, and operations like insertion, search, and removal. The speaker explains dictionary comprehensions and iterating through dictionaries in various ways. They then move on to discuss Python's heap implementation using the 'heapq' module, explaining how to create and manipulate min and max heaps. The paragraph concludes with an introduction to Python functions, nested functions, and the use of 'nonlocal' to modify variables outside the local scope. The speaker also briefly touches on Python classes, constructors, and methods, emphasizing the use of 'self' to access member variables and methods.
🎓 Wrapping Up and Resources for Coding Interviews
In the concluding part, the speaker summarizes the key Python concepts and data structures necessary for coding interviews. They stress that while the list may seem extensive, frequent practice makes it easier to recall and use these concepts during interviews. The speaker reassures viewers that it's normal to refer back to resources for syntax or specific usage and encourages continued practice. They also promote their website, neco.io, as a platform for further learning and practice, offering video explanations and code support in multiple programming languages. The speaker thanks the audience and expresses hope to see them in future sessions.
Mindmap
Keywords
💡Python
💡Coding Interviews
💡Dynamically Typed
💡None
💡Indentation
💡Data Structures
💡List Comprehension
💡Recursion
💡Classes
💡Heaps
Highlights
Introduction to Python for coding interviews
Background of the channel creator and their journey to Google
Advantages of Python in coding interviews
Python's dynamic typing and variable declaration
Differences in incrementing variables in Python
Python's handling of 'None' and its use as a null value
Conditional statements in Python without parentheses
Python's 'and' and 'or' keywords for logical operations
While loops in Python and their syntax
For loops and range functionality in Python
Negative indexing and slicing in Python lists
List comprehensions for initializing lists in Python
String immutability and operations in Python
Queue operations and deque module in Python
Set operations and their time complexities in Python
Hashmap or dictionary usage and operations in Python
Tuples as immutable sequences and their use as keys
Heap implementation and heapify function in Python
Functions in Python and nested functions for recursion
Classes in Python and their structure
Conclusion and resources for further learning
Transcripts
hey everyone welcome back and let's
write some more neat code today so today
I want to cover everything you need to
know about python for coding interviews
if you're new here a little bit about my
background I started the channel a
couple years ago at the beginning of the
pandemic I was unemployed and I made
solutions to coding interview questions
in python and a little over a year after
that I eventually got a job at Google
and I pretty much used python throughout
all my interviews and I think it was
really helpful because python is so much
easier than most languages it's super
concise I've actually never written a
line of python code at any jobs or
internships I've ever had I literally
learned python just for coding
interviews and it's definitely been
worth it if you're already familiar with
python I hope you learned some tips and
tricks from this video and if you use
other languages like Java or C plus plus
I hope by the end of this video you see
the benefits and then join the church of
python I think you'll be able to learn
it faster than you expect especially if
you're already familiar with programming
and by the way if you're studying for
coding interviews check out neco.io it's
a side I created has a bunch of free
resources to help you prepare and I've
also started making courses I just
finished up the advanced algorithms
course and I'm going to get started on
the system design course for beginners
so you definitely don't want to miss out
on that okay now let's get started the
first thing you should know about python
is that it's a dynamically typed
language so when we declare a variable
like n and set it to zero we don't have
to declare the type at all so now if we
run the code in print and you can see
it's equal to zero but types are
determined at run time so we can
reassign n to a string now and that's
perfectly fine because n has no type the
type is determined at run time so now we
can run the code again and we can see
that n was originally a zero and then it
changed to a string ABC we can also do
multiple assignments but it's a bit
different than most languages if we have
two variables we put them both on the
left hand side and then we have our
equal sign and then we put the two
values on the right side it's okay to
have multiple types in a single line
line incrementing is a bit different of
course we can increment a very simple
way like this n is equal to n plus one
we can also do the shorthand and plus
equal to one just like most languages
but we can't do plus plus you can see it
even gives us a syntax error we can't do
plus plus that's related to the python
interpreter but it's not a big deal I
mean this is one of the few cases where
python is a bit less concise python also
has null but it's called none in Python
which is basically the absence of a
value unlike most languages though we
can have a value that's initially a
number or it's supposed to store a
number like four but then we can
reassign It To None which means null and
then we see it is equal to none if
statements are pretty straightforward in
Python at least conceptually but
syntactically there's a couple
differences namely that we don't need
parentheses so if we have an if
statement like this we don't need to put
the conditional in parentheses and we
don't need curly braces to represent
what is the block that corresponds to
this conditional we use indentation
station for that so you can see here we
have a tab that indicates that this code
belongs to this statement we have a
colon that goes after the conditional
else if Works a bit differently we don't
actually have both keywords else if we
even shorten that I don't really care
too much for this but I'm guessing it
just has to do with the python
interpreter because we already have two
keywords for if and else so we have to
create a new keyword for else if at
least that's what I'm guessing while
parentheses aren't required for
conditionals in Python they are needed
if we have multi-line conditionals and
by the way logic and in most languages
is the double Ampersand character but in
Python it's just the keyword and or is
just the keyword or so not that this is
shorter but it just makes things a
little bit more readable for example if
we have an if statement that looks like
this we use the keyword and and we use
the keyword or to mean logic and and
logic war and since we have a multi-line
conditional like this these two lines
are part of the conditional we have to
put them inside of parentheses otherwise
if we get a syntax error syntax
practically while Loops are pretty
similar for example we have a variable n
equals zero we can create a while loop
where the condition doesn't have to go
in parentheses the block is followed by
a colon and the code itself is indented
with a tab we're printing n so running
the code we get to print zero through
four four Loops are pretty similar so if
we want to do the exact same thing go
from zero through four we create a for
Loop where our variable I is going to go
in the range of five that basically
means it's going to start at zero and
keep going until it reaches 5 and then
stop and I is incremented implicitly so
we don't have to tell this Loop to
increment I I is just going to be
incremented on every iteration of the
loop by default so running the code
you'll see we get pretty much the exact
same thing zero through four so five is
not included as the loop execute to
better illustrate the for Loop let's
take a look at another example where
we're going from two through five so in
that case we'd use the same keywords for
I in range but we'd pass in two values
into range we're starting at two and
we're going up until six but not
including six so now when we print this
we go from two all the way up until five
and if you want to go in reverse
starting at five and going down to two
it would be similar we'd start at five
we'd go up until one but not including
one and we'd pass in a negative one as
the third argument because in this case
we're decrementing if you don't pass in
a negative one we increment the I but
when you pass in negative one that means
we're decrementing the I and we actually
could have passed in a negative two here
as well if we wanted to decrement by two
every single time but just decrementing
this going from five down to two and you
can see we get what we expected I will
admit this was one of the annoying
things when I first learned python
compared to most languages where you can
do something like this explicitly
declare the variable explicitly State
the condition and explicitly increment
our decrement this python syntax is
different definitely a bit trickier but
I think as you get used to it it's
easier to type out than this bottom
stuff that I have here division is also
a bit tricky in Python it's decimal
division by default whereas most
languages use integer division so 5
divided by 2 will actually give us 2.5
whereas in most languages it'll round
towards Zero by default if you want
integer division you have to use double
slash like this so printing this will
actually round down you can see we get
two as the result but if you caught what
I just said you have to be careful
because most languages will round
towards Zero by default but in Python if
we do integer division we're rounding
down so you can see negative 3 divided
by 2 will actually round down to be
negative 2 where the decimal value would
be negative 1.5 in most languages there
would be negative one a workaround for
this is to use decimal division but then
convert the result to an integer because
when you convert to an integer it will
round towards zero this is kind of a
annoying but it's just something you
have to do you can see running this will
give us a negative one which is what you
might want in certain cases though I'll
mention it's pretty rare to have to need
to know this I'm just mentioning it in
case you run into any issues using the
modulo operator is pretty similar to
most languages so 10 divided by three
we're getting the remainder we would
expect a one and that's exactly what we
get except the issue is once again with
negative values just like dividing
negative values when we mod negative
values we get unexpected results and
negative 10 modded by three well the
answer is actually two and this is
different from most C based languages
like Java C plus plus I think even
JavaScript so if you want to be
consistent with those other languages
you can import math and then do F mods
using this will give us negative one
which is what you might have expected a
few more useful math helpers we have
floor which will explicitly round down
we can also do the opposite which is the
ceiling so three divided by two rounding
up if you need to take the square root
there's a Helper and if you need to take
the power of a variable raised to
another so 2 to the power of 3 would be
this if you ever need a maximum integer
you can use float infinity and if you
ever need a minimum integer you can use
float negative infinity and part of the
reason that they come up is because
python numbers are infinite so they
actually never overflow so if we have a
number like this 2 to the power of 200
which is a very large number as you can
see on the right after we print it it's
so large that we can't even print it
it's 60 additional digits but even this
large integer is still less than
infinity checking if this number is less
than infinity we get true arrays which
are called lists in Python are probably
the most common data structure you're
going to use next to Hash Maps so an
array can be declared just like this but
initializing it is pretty
straightforward you just put the values
inside of some brackets and printing it
is just as simple arrays in Python are
Dynamic arrays by Define fault so just
like in most languages Dynamic arrays
can be used as Stacks so you can push to
the array AKA append and you can also
pop from the array which will pop from
the end of course so after we push a
four and we push a five we can print
that and then after we pop the last
value you can see that we popped the
five because this is technically an
array and not a stack we can actually
insert into the middle so at index one
we can insert a value seven printing
that you can see we indeed inserted a 7
into the middle of the array but unlike
pushing and popping from an array
inserting into the middle is a big O of
end time operation but it's not a big O
of end time operation to index an array
so at index 0 we can read the value and
we can also reassign the value to zero
in this case and we can reassign the
value at index three and these
operations are constant time operations
to initialize an array of variable size
let's say we wanted an array of size
five and we wanted all values to be 1
one we could do it pretty easily it
might be kind of weird to use the
multiplication operator here but
syntactically it's pretty easy printing
the array and the length of the array
you can see we do get what we expect but
be careful when indexing an array
especially when you're using negative
values because negative one is actually
not out of bounds in Python negative one
will actually read the last value as you
can see on the right and to read the
second to last value you can use
negative two now in my opinion this
isn't super useful but sometimes it can
be when you want to quickly read the
last value getting sub lists AKA slicing
an array is one of the most useful
features of python so here we're taking
the values of the array from index 1 to
index 3 but not including index three
just like with for loops and then
printing those values so we would expect
two and three and that's what we get and
we could also go from index 0 to 4 which
is pretty much the entire array and
that's valid as well unpacking is also a
super useful feature basically we can
take all the individual elements of an
array and assign them to variables in
this case ABC this can be super helpful
when you want to go through a list of
pairs for example be careful though
because you do have to make sure that
the number of variables on the left hand
side does match the number that you're
expecting from the array we can Loop
through arrays in many different ways
using the simple for Loop syntax we
talked about earlier we can take the
length of the array and then iterate
that many times using an index I and
then printing the individual value an
easier way to accomplish the exact same
thing is without using an index so we
can actually go through every value in
nums and then just print that individual
value if for some reason you needed both
the index and the value you could use
the first for loop I have shown up here
but another way to do it is to use the
enumerate function in Python so a
numerate will actually give you the
index which will be the first value
that's unpacked and the second value
that's unpacked will be the number and
then we can print both of those if for
some reason we needed both of them and
all three of these Loops execute as you
would expect now if we want to iterate
through multiple arrays simultaneously
we can do that with unpacking and a
helper function called Zip Zip will
basically take both of these arrays and
combine them into an array of pairs and
then we can unpack those pair of values
which are values from nums 1 and nums
two we get pretty much what we expect
reversing an array is as simple as
calling the reverse method on that array
so one two three becomes three two one
sorting an array is just as easy taking
this array we can call sort on it so
this will sort it in ascending order by
default if we want to sort it in reverse
order we just pass in the parameter
reverse equals true and then the array
will be sorted in descending order we
can also sort a list of strings by
default they will be sorted based on
alphabetical order as you can see on the
right but if we want to implement a
custom sort for example if we want to
sort based on the length of each string
then we can do that by passing in a
Lambda function so in this case the key
is equal to Lambda which is basically a
function without a name and we're going
to take every single value from the
array call it X and then return from
that the length of X and this is the key
that's going to be used to sort the
string so each string is going to be
mapped to its length and then we're
going to sort those strings based on
their length so by default it's going to
be in ascending order as we can confirm
on the right another sort of advanced
way to initialize lists is using list
comprehension so if we wanted to go
through every value in range 5 and to
call that value I and we want to add
that value to this array this is the
shorthand so we're iterating for I in
range 5 and then I is going here so
we're taking that I value and adding it
to the array and printing it you can see
that we indeed have zero through four
now maybe we want to go through every
value in that range but we want to take
I and add I plus I to the result so for
every index we want 2 times that index
added to the result you can see we can
also do that pretty easily if you want
to do something similar for a 2d list
it's also pretty easy but maybe a bit
different than you would expect the
easiest way to do it is the shorthand
that we talked about earlier where we
take an array with zero and then
multiply it by four this will give us an
array of size 4 with all zeros and we
want this array to be added to the outer
array for time so we have an inner loop
for I in range four we're not even using
the variable I here but this will build
a 4x4 grid of all zeros you might be
thinking isn't there an easier way to do
that well actually not you might be
thinking can we just do this create an
array of size 4 and then multiply that
by four well technically this will work
but each of the four rows of this array
are going to be the same so if we modify
one of the rows we're going to be
modifying all of the other rows we're
not actually creating four unique rows
in this case it's a common thing that
can throw people off if you have more
questions about this feel free to ask in
the comments it's something that tripped
me up a lot when I first started strings
are pretty similar to arrays so we can
declare one with double quotes you can
also use single quotes if you want and
we can slice them the same way we do
with arrays and printing it works the
same but a key point is that they are
immutable that means we can't modify
this string that means we can't reassign
the character at index zero we can
however update the string but updating
it will actually create a new string so
adding def to the end of the string will
create a new string so basically any
time you modify a string it's considered
an end time operation strings can be
converted into integers and then those
integers can be added integers can also
be converted into Strings and then those
strings can be added together so when
you add two integers together we get an
integer as the result adding two strings
together appends those strings together
so we get one two three one two three if
for some reason you need the ASCII value
of a character you can do that with the
ORD function so printing this you can
see we get 97 is the ASCII value of
lowercase a 98 is the ASCII value of
lowercase b you can also join a list of
strings together with a delimiter in
this example we have three strings and
we're joining them with the empty string
delimiter here so we're basically just
appending these three strings together
we could also have had a delimiter maybe
a space in between all of them and
printing the result we get all three
strings appended cues in Python are
double ended cues by default you can
import them adding values to the right
side is as easy as appending to the
queue so at this point our queue isn't
much different from a stack but the
benefit is that we can actually pop from
the left of the queue and we can do this
operation in constant time unlike with a
stack as you can confirm on the right
since it's double ended we can also add
values to the left of the queue so the
one that we popped we can add back to
the left side and also we can choose to
pop from the right side if we want to so
running this we can confirm that the one
is added back and then we pop the two
after that Ash sets are really useful
because we can search them in constant
time and we can insert values also in
constant time of course there won't be
any duplicates in our set of course
unlike a list there can't be any
duplicates in a hash set but we can just
as easily get the length of the hash set
to know how many elements have been
inserted we can also search the hash set
without a function we can use the in
operator so if we want to know if one
exists in the hashtag same thing with
two same thing with three which we know
does not exist in the hash set as we can
confirm we can remove values also in
constant time time and confirm that the
value has indeed been removed to
initialize a hash set with a bunch of
values we can actually pass in a list
but just like with lists we can also do
set comprehension and manually
initialize it with a loop inside of the
hash set so here we're going through
every value in the range of I and taking
that value I adding it to the hash set
and initializing it this way is
identical hashmaps are probably the
single most common data structure you're
going to be used and this is what we
were saving those curly braces for to
insert we simply take some key value in
this case a string and assign it to
another value in this case a number 88
and we can add a bunch more just like
with hash sets we can't have duplicate
keys inside of the hash map printing it
is just as simple taking the length will
give us the number of keys that exist in
our hash map we can modify the value
that's mapped to a key so we can change
Alice from being 88 to 80. we can also
search if a key exists in a hashmap in
constant time and we can also remove
that key which will also remove the
value as we can confirm on the right to
initialize a hashmap we can add pairs
inside of the curly braces where each
pair is separated by a comma and the key
goes on the left side of the colon and
the value goes on the right side this is
the same as manually inserting values
into the hash map but if you want to get
even more fancy you can use dict
comprehension hash maps are basically
called dictionaries in Python and the
syntax is pretty similar but in this
case if our looping I in the range of 3
we're going to have two values I in this
case is the key then a colon and then
the value goes after that in this case
we're mapping I to 2 times I this is
pretty powerful and I find that I use it
most frequently when I'm doing graph
problems and trying to build like an
adjacency list looping through a map is
pretty interesting because there's many
ways to do it by default we iterate
through every single key and then we can
you know print that key and also print
the value that that key maps to but also
we can directly iterate through the list
of values of that hash map if we we
don't even need the key lastly using
unpacking we can actually go through the
items of that map which will give us the
key and the value this is pretty similar
to the first Loop that we have I guess
it's a bit more concise to write it this
way python also has tuples which are
pretty similar to arrays except to
initialize them we use parentheses
rather than brackets and they are
immutable so while we can index them we
can't modify them so this won't work
you'll mainly be using tuples as keys
for a hash map or a hash set so in this
case we're mapping a pair of values one
two to three so this Tuple is basically
our hashable key we can do the same
thing for hash sets of course and then
we can use that Tuple to search the hash
set the reason we do this is because
lists are not hashable and can't be keys
for hash sets or hash Maps so this here
will not work heaps are another really
common data structure to find the Min
and Max of a set of values frequently
under the hood in Python they're
implemented with array days of course so
actually to create an empty Heap we just
create an empty list and to push values
to that Heap we use Heap Q dot heat push
to that Min Heap the value three by
default heaps in Python are Min heaps so
we push a few more values and then to
get the minimum value it'll always be at
index 0. that's just how heaps are
implemented to Loop through a heap while
the length of the Heap is non-zero we
can also pop values from the Heap with
Heap Q dot heat pop from that Min heat
and then print the corresponding value
that we just popped since it's a Min
Heap we'll see the values are printed
from smallest to largest while python
doesn't have Max heaps by default the
workaround is basically to multiply each
value that we push by negative one and
then after we pop that value we also
multiply it by negative one to negate
the original negative one so if we
wanted to implement a Max Heap and push
the value 3 we would actually push
negative three and if we wanted to push
two we'd push negative two same thing
with four again the max will always be
at index 0 but we know we have to
multiply it by negative one to negate
the original negative one by popping
each value and multiplying it by
negative one we can confirm that the
values are printed from greatest to
smallest now if you already have the
initial set of values that you want to
build the Heap from you can do it in
linear Time by calling build Heap or in
Python it's called heapify so we can
call Heap Q dot heapify this array and
while that array is not empty we're
going to keep printing the values and we
can confirm that they're printed from
smallest to largest functions in Python
are pretty straightforward and concisely
use the def keyword we name the function
and then we pass in some parameters in
this case n and M just like with
conditionals and Loops we use a colon
after the Declaration of the function
and the body of the function is going to
be indented so we're returning the
multiplication of those two values and
printing the result one functionality I
use a lot in coding interviews is nested
functions this can be really helpful in
recursive problems because if you have
an outer function that takes in a couple
parameters and you also declare some
values in that outer function the inner
function will actually have access to
all of those variables by default so
then if we called the inner function we
don't even have to pass in a b and c now
this is a pretty simple example but if
you've watched any of my graph videos
you know how nice this can be in keeping
our code concise one thing that trips a
lot of people up with nested functions
is that you can modify objects but you
can't reassign values unless you use the
non-local keyword so if we have a
function that's going to double every
value inside of an array and also double
this value itself this is not an array
we can have a helper function it'll have
access to both of those outer variables
we can modify the array pretty easily by
going through each value in the array
and then doubling it this works and will
update the original array but if we
doubled the value it'll only double the
value in the scope of the helper
function if you want to update the value
outside of the helper scope you'll have
to declare it as a non-local value and
doing this and and then modifying the
value will modify the original values
and then in the outer function we can
call the helper function without passing
in the variables and then print the
variables not actually called the double
function we can create some variables
call it run the code and we can see that
each variable was doubled again this is
a trivial example but if you're familiar
with my videos you know that this can be
helpful classes are also pretty concise
but a bit more limited than other
languages a Constructor is basically
double underscore init double underscore
after that that's kind of the name of
the Constructor in Python self is passed
into every method of a class it's
basically like the this keyword in other
languages in this case our Constructor
is maybe taking a list of numbers to
create member variables we also use the
self keyword so this is creating a
member variable called nums and
assigning it to the nums that were
passed in as a parameter to the
Constructor we can also create a member
variable for the size of nums by taking
the length of the parameter to create a
method for this class class for example
get length we don't want to pass in any
parameters to this but we have to pass
in the self keyword always that'll give
us access to Our member variable which
we're going to return self.size if we
want to call another member variable
from a member variable in this case we
want to call get length from this other
function get double length we can do
that again with the self keyword this is
a pretty useless example but I'm mainly
trying to explain the syntax and the
structure of classes in Python now this
is more or less everything I've needed
to know for coding interviews it's
surprisingly not a lot and you don't
have to memorize any of this as you
solve coding problems and as you prepare
for interviews you might have to look up
the syntax or how exactly do I use heaps
how exactly do you use double ended cues
things like that it's perfectly okay but
after you practice enough most of this
stuff is pretty easy to get down it
doesn't even feel like you're writing
code after a while python is a pretty
big reason I was able to get a job at
Google in my opinion now if you're
preparing for coding interviews check
out neco.io there's is a ton of free
resources to help you prepare each
practice problem listed has a thorough
video explanation and we have code
support for python C plus plus Java and
JavaScript if you're new to data
structures and algorithms I've got some
really helpful courses not only for
beginners but also for advanced users
thank you so much for watching and
hopefully I'll see you pretty soon
Voir Plus de Vidéos Connexes
Roadmap 🛣️ of DSA | Syllabus of Data structure | Data Structure for Beginners
Sequence_data_part_1
Introduction to Python
How to Start Leetcode (as a beginner)
How To Get Placement After Wasting 2-3 Years of Engineering? SDE Roadmap for College Students
How to start DSA from scratch? Important Topics for Placements? Language to choose? DSA Syllabus A-Z
5.0 / 5 (0 votes)