Performing mathematical operations in Python:


To add two numbers:

100+20


To subtract two numbers:

200-40


To multiply two numbers:

45*67


To perform complex mathematical operations:

4*6*(28+5)


There are two ways to divide numbers in Python:

200/2

Using a single slash will give a float value:

A float is a value that has a decimal point.


To obtain an integer value instead of a float, use "//" instead of "/":

200//2

This is called floor division.


Dividing a number by zero will result in a divide by zero error:

345/0

Always ensure that you do not divide any number by zero in your program, as it will result in an error.

When you multiply a float value with an integer value, the result is always in float:

For example, if we write:

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.


Complex mathematical operations:

An exponent refers to how many times a number is multiplied by itself. For example, 5 is multiplied by itself 3 times i.e. 5 x 5 x 5, then we can say it is 5 raised to 3. We also call it 5 to the power 3 or 5 raised to 3. Here, 3 is the exponent and 5 is the base.

Examples of Exponent in Python:

We can calculate exponents in Python using the "**" operator. For instance:


To calculate 5 to the power of 3, we write:

5**3


To calculate 2 to the power of 4, we write:

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 "%".


The Modulus Operator:

The modulus operator "%" divides the two numbers and gives us the remainder instead of the quotient. The modulus operator is specifically useful when we need to check if a number is odd or even. If any number % 2 gives zero as the remainder, the number is odd. If it gives 1 as the remainder, the number is even. The modulus operator can also be used with floating-point values.

Example:

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.

'He\'s a good guy'

This tells Python that ' is not a part of syntax.


Another way to print the above sentence is by using double quotes instead of single ones.

"He's a good guy"

Now here, as we have used double quotes, the single quote is automatically escaped as Python does not treat it as a part of syntax. It believes that the string starts when we start " and ends when we add another " and everything in between is a string.


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:

'This is the "only" way to go'


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:

"This is the "only" way to go"

We need to fix this by using the backslash escape character.

"This is the \"only\" way to go"


Accepting user input:

In some programs, it is necessary to accept input data.

Python provides a function called input() to accomplish this.

Let's try using this function.

input()

This will prompt us to enter something.


Let's enter some input.

If we enter our name, the output will be the entered value.

However, when we execute the input() function, we may not be sure what the program expects us to do or what value should be entered. Therefore, we can pass a string argument to the input() function to prompt the user for input.

For example, we could use:

input('Enter your name')

After entering the name, it will be displayed.

We can also save this input as a variable for later use in our program, but we will cover that topic later when we learn about variables.


String operations:

When you enclose anything in single or double quotes, it becomes a string.

For example:

'Hello there'


Even a number or an integer becomes a string if it's enclosed in quotes:

'8'


To check the actual type of a value, you can use the type() function:

type(8)
type('8')


String operations include concatenation using the + operator:

"Hello" + " world"


You can also include spaces inside of the quotes:

"Hello " +  "World"


You can concatenate two numbers as a string:

"10" + "10"


If you try to add two numbers without quotes:

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:

"Hello " + "5"


But if you try to concatenate a string with an integer, you will get an error:

"Hello" + 5


To multiply a string with a number, you can use the * operator:

"Hello " * 4

This will output "Hello Hello Hello Hello ".

However, you cannot multiply a string with another string.


Data types in Python:

When we write a program, we need to work with data. This data needs to be stored somewhere, and hence, to store this data, we use variables.


Let's create a variable and assign it a value:

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.


To print the value of "a", we can use the print() function:

print(a)


Let's create another variable "b":

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.


The value of a variable can be changed at any instance:

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)


Naming Python variables:

You can use long or short variable names in Python, but it is advisable to write readable variable names so that we can understand what is stored in them. For example, when writing a program to calculate the area of a circle, it is better to name a variable "radius" rather than just "r" to save the radius value.

In Python, variables can be of any length and can consist of uppercase letters, lowercase letters, digits, and underscores. However, the first character of a variable cannot be a number. Let's take a couple of examples:

radius = 10.   # Allowed
ra20dius= 20   # Allowed
10radius = 30. # Not allowed
1radius=20.    # Not allowed
_radius=10.    # Allowed
circle_radius = 10 # Allowed


Another restriction on variable names is that we cannot use Python keywords as variable names. Keywords are part of the Python programming language itself. For example, the print() function is a keyword. Let's try using "print" as a variable:

print = 20

This will result in an error.