Beginners Python Walkthrough Part-1

1. Why Learn Python In The First Place?

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:

1.1) Hello World program in Java

class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
File "<ipython-input-2-4177b436845e>", line 1 class Main { ^ SyntaxError: invalid syntax

1.2) Hello World program in Python

print("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:

  • Data Analyst
  • Python Developer
  • Backend Developer

And if you are someone with a some experience on hand then you can apply to the likes of:

  • DevOps Engineer
  • Data Scientist
  • MLOps Engineer
  • AI Scientist, etc.

2. Getting Started With print() Function

2.1) Printing without the quotes

Assume 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

2.2) Printing with the quotes

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

3) Mathematical Operators

Python supports all the 4 general mathematical operators

3.1) Addition

  • The + symbol denotes addition.
  • It returns the sum of two or more numbers
print(4 + 5)
print(2.5 + 2.5)

3.2) Subtraction

  • The - symbol denotes subtraction.
  • It returns the difference of two or more numbers
print(8 - 5)
print(7.0 - 5.5)

3.3) Multiplication

  • The * symbol denotes multiplication.
  • It returns the product of two or more numbers
print(2 * 8)
print(9 * 0.5)
Division
  • The / symbol denotes division.
  • It returns the quotient of two numbers.

4) Variables

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

  1. A variable names should only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  2. Variable names are case-sensitive (A personal advice is to use lowercase only).
  3. Variable names cannot start with numbers.

4.1) Expressions

A valid combination of numbers, variables, and operators called an expression. Example:

  • (7 * 4) + (67 - 2) = a
  • x + 45 = 67
  • x / y = 876

The conventional order of solving a mathematical expression always follows the BODMAS RULE:

  1. Bracket(B)
  2. Order(O)
  3. Division(D)
  4. Multiplication(M)
  5. Addition(A)
  6. Subtraction(S)

Here's a step-by-step evaluation of an expression:

  1. (7 * 4) + (67 - 2) - (88 / 4)
  2. (28) + (65) - (22)
  3. 93 - 22
  4. 71

5) Data Types

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:

  1. String
  2. Integer
  3. Float
  4. Boolean

5.1) String (denoted by str)

A string is defined as continuous sequence of letters, numbers, alphanumeric characters enclosed within quotes. This can include the likes of:

  • Capital Letters ( "A – Z" )
  • Small Letters ( "a – z" )
  • Digits ( "0 – 9" )
  • Special Characters ("~ ! @ # % ^ . ?,")
  • Space (" ")

5.2) Integer (denoted by int)

An integer is defined as any whole numbers(positive / negative / 0) without any fractional or decimal parts. This can include the likes of:

  • Positive Numbers (88,34,3,21,123)
  • Negative Numbers (-123,-34,-567,-76)
  • Positive nor Negative ( 0 )

5.3) Float (denoted by int)

Any numeric value with a fractional part or decimal point comes under the float datatype.
This can include the likes of:

  • Positive Decimal Numbers (67.56,367.56)
  • Negative Decimal Numbers (-13.98,-534.87)

5.4) Boolean (denoted by bool)

In a broad sense, everything that can hold one of two possible values is called a Boolean. This can include the likes of:

  • True or False
  • On or Off
  • 0 or 1
  • Yes or No

Note: Python programming language recognizes True and False as the boolean values.

6) Order of Execution

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
  1. Python returned the output "Hyderabad" as the variable & its value were initialized before the corresponding print() function was called.
  2. Python returned a "NameError" because the variable "grade" had not yet been created when we attempted to print it.

7) Continuous Variable Updation

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

8) Taking Inputs From Users

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+ "!")

9) Working With Strings

9.1) String Concatenation

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)

9.2) String Repetition

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)

9.3) String Indexing

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)

9.4) String Slicing

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)

9.5) Slicing from Start

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)

9.6) Slicing to End

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.

10) Type Conversions

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))

10.1) String datatype to Integer datatype

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)
  • Case 1: When we did not convert the input values to integer datatype, python by-default considered it to be strings. And when we added them, it concatenated the two strings and returned the output 2424.
  • Case 2: When we converted the input values to integer datatype, we got the expected sum of two numbers as python treated them as integers returned the output 48.

10.2) Commonly used type conversions

  1. int() : Converts to integer datatype
  2. float() : Converts to float datatype
  3. str() : COnverts to string datatype
  4. bool() : Converts to boolean datatype
  1. In Part 1 of this blog, we've covered all of the fundamental must-know topics for getting started with Python programming for data science.
  2. Do look out for the upcoming Part 2, in which we will look at these following topics in depth:
  • 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...
 
vigilantstars6
Vishnu Arun6 months ago
Jovian
Sign In