Section 5 Notes
Section 5 Notes
• Python is a GPL that can be used to write desktop software, web applications, data science, ML,
etc.
• In contrast to general-purpose programming languages, we also have domain-specific
programming languages, such as SQL.
• DSLs are used within a specific area, such as SQL, which can only be used for querying relational
databases and nothing else.
• Indentation makes code more readable, but in programming languages like C, C++, and Java,
indentation is only used for code readability and formatting.
• In Python, indentation is part of the syntax and is used to indicate a block of code.
• For example, in Java, we use curly braces to represent a block of code, whereas in Python, we
use indentation, i.e., spaces to indicate a block of code.
• In Python, you do not have to declare the type while creating a variable.
• Due to dynamic typing, a variable can have a different type at different times during execution.
• In dynamic typing, the type of variable is decided at runtime.
• In Python, we do not have to worry about freeing up or reclaiming allocated and unused
memory because it is garbage collected.
• Garbage collection automatically reclaims memory that was allocated by the program but is no
longer in use.
• As programmers, we do not have to manually deallocate memory.
Section Notes
• 100+20
• 200-40
• 45*67
• 4*6*(28+5)
• 200/2
Using a single slash will give a float value:
A float is a value that has a decimal point.
• 200//2
This is called floor division.
• 12.0 * 4
This will give us 36.0 instead of 36.
However, if we write:
• 12*4
It will give us an integer value.
• 5**3
• 2**4
We can also use exponents to calculate the square root of a number, for instance:
• 64**(1/2)
Finding Remainder in Division:
When we divide two numbers in Python, we only get the quotient. However, in division,
we have a remainder as well. For example, if we say:
• 21/4
We should get 1 as a remainder. Hence, to specifically get a remainder, we use the
modulus operator "%".
• 456.34%2
Strings in Python:
To create and print a string:
• print('Hello world')
When we enclose anything in single or double quotes, it becomes a string in Python.
While using the Python interactive console, you don't have to use the print statement to
print the string.
You could directly enclose anything inside of single or double quotes and that would be
printed. For example:
• 'hello world'
Hit enter and you will get "hello world" printed. The same could be done using double
quotes as well.
Let's try printing a sentence like "He's a good guy".
When we try doing that, we get an error.
This error occurs because Python thinks that the single quote which we use in the
sentence is not a part of the sentence but instead is a part of the syntax.
Python thinks that the string starts at ' and ends at '.
Hence, to tell Python that ' is a part of a string, we use something called an escape
character which is \ backslash.
To specify that we want to escape a character, we add a backslash before it. In this
case, we want to escape the ' in the sentence and hence we add a \ before it.
Another way to print the above sentence is by using double quotes instead of single
ones.
The same could be done with single quotes as well. If we use single quotes to represent
a string, we could use double quotes inside a string. For example:
But instead of the above, if you use double quotes for creating strings as well as inside
of a string as well, in that case, we will get an issue. Let's try doing that:
• input()
This will prompt us to enter something.
String operations:
When you enclose anything in single or double quotes, it becomes a string.
For example:
• 'Hello there'
• '8'
To check the actual type of a value, you can use the type() function:
• type(8)
• type('8')
• "10" + "10"
• 10+10
Python treats them as integer values and performs addition instead of concatenation.
You can also concatenate a string and a number by making the number a string:
But if you try to concatenate a string with an integer, you will get an error:
• "Hello" + 5
• "Hello " * 4
This will output "Hello Hello Hello Hello ".
However, you cannot multiply a string with another string.
• a = 200
We have created a variable named "a" and assigned the value 200 to it. The "=" sign is
called a variable assignment.
When we create a variable and assign it a value, we are essentially storing the value into
a memory location.
• print(a)
• b = 20
However, if you close the window and try accessing the value of "a" and "b", you won't
be able to do so. When we close the interpreter, the value stored in the variable gets
deleted.
• a = 100
• print(a)
• a = 200
• print(a)
Not just numbers, but you can also store other data types in a variable as well:
Strings:
• name = "John"
• type(name)
Floats:
• a = 3.2
• type(a)
Booleans:
• b = True
• type(b)
• print = 20
This will result in an error.