Python is a powerful programming language yet extremely simple to learn. It is built around the philosophy of minimal code to get the same work done. This makes it a forerunner among other programming languages for extensive usage in the domains of data science and machine learning.
Let's actually get an intuition of the same using an example:
Hello World
program in Javaclass Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
File "<ipython-input-2-4177b436845e>", line 1
class Main {
^
SyntaxError: invalid syntax
Hello World
program in Pythonprint("Hello World")
Yes, thats about it. We literally got the same output in a single line of code! In contrary, that same output needed required 5 lines of code in Java. Due to this compact and versatile nature of Python programming language, it finds its ways into complex domains like Machine Learning, Data Science, Big Data, AI, etc.
Moreover by getting proficient in python programming language in addition to some specific libraries and skills, its not wrong to say that you can apply to a plethora of entry level jobs like:
And if you are someone with a some experience on hand then you can apply to the likes of:
print()
FunctionAssume you want to print the numerical output of a mathematical expression. For this simply enter the values in the print function without the quotes. Lets look at an example of numeric sum of 7 + 3
print(7+3)
So it returns the numerical sum of the two values; 10
Assume you want to output the exact same value you entered into the print function. Simply insert the values in the print function within the quotations to do this. Consider this example sum of 7 + 3
print("7 + 3")
So it returns the expression 7 + 3 as the output. So, its kinda right to say that anything put inside the quotations will be treated as a string by python
Python supports all the 4 general mathematical operators
+
symbol denotes addition.sum
of two or more numbersprint(4 + 5)
print(2.5 + 2.5)
-
symbol denotes subtraction.difference
of two or more numbersprint(8 - 5)
print(7.0 - 5.5)
*
symbol denotes multiplication.product
of two or more numbersprint(2 * 8)
print(9 * 0.5)
/
symbol denotes division.quotient
of two numbers.A Python variable is a designated memory location used for storing values. We can also visualize variables to be empty containers which store user-defined values or information. Let us now initalize a varible named Pokemon
and give it a value Mewtwo
pokemon_legendary="Arceus"
unstoppable_train=777
print(pokemon_legendary)
print(type(pokemon_legendary))
print(unstoppable_train)
print(type(unstoppable_train))
When the variable is initalized, it means that it has been allocated some space within the memory. And when its given a value like Arceus
it gets stored in this stipulated memory location. Here the equals to sign ( = )
is called as Assignment Operator
as it is used to assign values to variables.
Note: Rules to follow when naming Python Variables
A valid combination of numbers, variables, and operators called an expression. Example:
The conventional order of solving a mathematical expression always follows the BODMAS RULE:
Here's a step-by-step evaluation of an expression:
Programming languages require that every user input be classified into a certain datatype in order for computers to understand it and act on it. Some of the commonly used built-in datatypes are:
str
)A string is defined as continuous sequence of letters, numbers, alphanumeric characters enclosed within quotes. This can include the likes of:
int
)An integer is defined as any whole numbers(positive / negative / 0) without any fractional or decimal parts. This can include the likes of:
int
)Any numeric value with a fractional part or decimal point comes under the float datatype.
This can include the likes of:
bool
)In a broad sense, everything that can hold one of two possible values is called a Boolean. This can include the likes of:
Note: Python programming language recognizes True
and False
as the boolean values.
Python always executes programs in a line-by-line sequence of order of code in a program.
name='Ram'
city='Hyderabad'
occupation='Student'
print(city)
print(grade)
grade=10
"Hyderabad"
as the variable & its value were initialized before the corresponding print() function was called."NameError"
because the variable "grade"
had not yet been created when we attempted to print it.In python, a variable's value can be modified or updated dynamically based on the requirement of the program.
mileage = 456
print(mileage)
mileage = mileage + 100
print(mileage)
In the above example, variable mileage initially held the value 456
. When 100
is added to mileage, its value gets updated to 556
. Now, if the variable is used hereafter at any point in the code, it returns the value 556
To take inputs from users we can make use of the input()
function.
Note: The input()
function by-default categorizes every line of user input as a string
datatype, unless the user explicitly mentions a datatype.
username=input()
print("Welcome, you've signed in as" + " " +username+ "!")
Adding one or more strings together is called string concatenation. We've already seen and used concatenations in the prior Section 8: Taking Inputs From Users.
However, lets take a look at another intuitive example
monument="The" + " " + "White" + " " + "House"
print(monument)
We use the multiplication symbol or (*)
asterisk symbol to make repeat a certain part of a string repeat "n" number of times.
Example 1:
alibaba_says= "Open Sesame!\n" * 10
#the \n is called a newline or linebreak character, makes it print on a new line.
print(alibaba_says)
Example 2:
message= "The Voyager probe is amongst stars"
reality = ("*" * 5) + message + ("*" * 5)
print(reality)
The positions of characters in a string, which always begins at 0 upwards
can be used to access and pullout specific characters from the string. These positions of these characters is referred as an index.
Example 1:
flipped_planet= "Uranus"
first_letter = flipped_planet[0]
second_letter = flipped_planet[1]
last_letter = flipped_planet[4]
print(first_letter)
print(second_letter)
print(last_letter)
Example 2 (Negative Indexing):
password="Shakalaka Boom Boom"
last_letter=password[-1]
second_last_letter=password[-2]
third_last_letter=password[-3]
fourth_last_letter=password[-4]
print(last_letter)
print(second_last_letter)
print(third_last_letter)
print(fourth_last_letter)
String slicing is the process of extracting a specific section of a string using indexing.
SYNTAX: variable_name[start_index : end_index]
NOTE: The end_index is not included in the output, meaning that python will stop at the end_index - 1th position.
Example:
message = "Houston we've had a problem"
part_message= message[5:10]
print(part_message)
If a start index is not specified, then python takes the default start value as index 0
.
Example:
message = "Houston we've had a problem"
part_message= message[:10]
print(part_message)
If a end index is not specified, then python goes till the end of the string.
Example:
message = "Houston we've had a problem"
part_message= message[8:]
print(part_message)
Note: If neither the start nor end index
are given then, python starts from the beginning and goes till the end of the string.
Type conversion, often known as type-casting, is the process of converting a value from one datatype to another.
Let us now make use of the type()
function to check the datatypes of some entries.
print(type("Let's Beyblade"))
print(type(3.14))
print(type(897))
print(type(True))
We use the int()
function to convert valid string data to integers.
Example 1:
user_otp = "346315"
update_otp = int(user_otp)
print(type(user_otp))
print(user_otp)
print(type(update_otp))
print(update_otp)
Example 2: Invalid Integer Conversion
number="Hundred"
convert_num= int(number)
print(convert_num)
Example 3: Adding two numbers
We will first examine what happens if the values are not converted to the int
datatype.
first_no = input()
second_no = input()
sum = first_no + second_no
print(sum)
first_no = int(input())
second_no = int(input())
sum = first_no + second_no
print(sum)
2424.
48.
int()
: Converts to integer datatypefloat()
: Converts to float datatypestr()
: COnverts to string datatypebool()
: Converts to boolean datatype
- Operators & Conditional Statements
- Nested Conditions
- Loops & Control Statements
- Lists
Until then take care, Happy Learning!
jovian.commit()
[jovian] Detected Colab notebook...
[jovian] Please enter your API key ( from https://jovian.ai/ ):
API KEY: ··········
[jovian] Uploading colab notebook to Jovian...