Python Basics: Basics of Python Programming

Basics of Python Programming

What is Python?

Python is one of the most popular general-purpose programming language which is used for almost all types of projects ranging from web development to mobile app development.

Even though the Python Language is among us for the last 30 years, it grew tremendously in popularity in the last few years due to the advancement of Data Science.

Why learn Python?

Python is one of the popular programming languages especially in the eyes of a beginner for various reasons. It has lots of features that make it different from other programming languages.

Python is easy to learn

This is the reason that attracts a new coder. Python features simple English syntax and is designed to be concise and easy to read. Learning this programming language makes you feel like learning English.

Also, it has the facility to run each line of code separately which is very helpful in bug finding and fixing the error on the spot.

Python is a versatile language

Python is a very flexible and versatile language that fits every requirement. One can use it to do any type of project like Web Development, Software Development, Mobile App Development, Data Science, and AI projects. It can be used to make as simple as QR code generator applications to complex face detection ML apps.

It also works nicely together with other programming languages which is very necessary for modern types of applications. You can combine Python with Java or C language together to make a highly customized project.

Lastly, it offers cross-platform functionality, meaning that it will function properly whether you’re working with Windows, Linux, or macOS.

Huge supportive community

Python has a huge support community and in fact, it is the second most popular language based on the internet community after Javascript.

Anywhere you are stuck in the code, just search it on the internet you will end up with lots of answers. Apart from that it has about  1.5 Million GitHub repositories where the user has done something related to python. Just imagine the number.

Huge demand for Python Developer

Python developer roles are in high demand, as many major companies are using this language. With the advancement of Data Analytics and Machine Learning, Python language is never used so vigorously in the industry as today making Python developer jobs comparatively lucrative.

Python developers earn an average salary of $108,591 in the united states and according to StackOverflow, Python developers also have the opportunity to earn a high salary with less experience, which is not the case with other programming languages.

Python Basics:

Comments in Python

Comments are used to give information or short descriptions about the piece of code to increase its readability. Comments are not compiler and are neglected by the compiler.

There can be single-line or multiple-line comments according to the needs. In Python, single-line comments are started by the ‘#’ sign and multiple-line comments are started and ended by ‘/*’ and ‘*/’ respectively as shown below.

Single Line Comments
# This is a comment in Python

Multiline Comments
This is a comment

 and it doesn’t interfere

 with the code

Indentation in Python

Indentation is nothing but the space at the beginning of a code line. In other programming languages, like C++, Javascript, etc., the Indentation is just for the readability of code while in Python, Identation is mandatory otherwise the code will throw IndentationError.

Different data types in Python

In Python, there are several built-in data types that can be used to store values. These are listed below:

  1. Integers: Integers are whole numbers that can be positive, negative, or zero. For example, 0, -5, and 100 are all integers. In Python, you can use the ‘int’ data type to store integers.
  2. Floating-point numbers: Also known as “floats,” these are numbers that have a decimal point. For example, 3.14, -10.5, and 0.0 are all floats. In Python, you can use the float data type to store floating-point numbers.
  3. Booleans: A boolean value is either ‘True’ or 'False'. In Python, you can use the ‘bool’ data type to store boolean values.
  4. Strings: A string is a series of characters, such as a word or a phrase. In Python, you can use the str data type to store strings.
  5. Lists: A list is an ordered collection of values that can be of any data type, including other lists. In Python, you can use the list data type to store lists.
  6. Tuples: A tuple is similar to a list, but it is immutable, meaning that its values cannot be modified once it is created. In Python, you can use the tuple data type to store tuples.
  7. Sets: A set is an unordered collection of unique values. In Python, you can use the set data type to store sets.
  8. Dictionaries: A dictionary is a collection of key-value pairs. In Python, you can use the dict data type to store dictionaries.

There is a lot to study in Lists, Tuples, Sets, and Dictionaries. You can read about all these on this page List, Tuples, Set, and Dictionaries in Python.

Print something in Python

To print something, Python has an inbuilt function named “print” which requires the thing to be written inside parenthesis. If you are printing any string, it is required to be closed by inverted commas double or single and if you are printing any numerical or through any variable, it does not require inverted commas.

print("Bond James Bond")


print('Bond James Bond')

Output:Bond James Bond

Variables in Python

Variables are like containers to store data. The function of variables is the same in any programming language not just in python. You first declare it to store some form of data and then call it for use. Below are some of the variable types.

price = 10 (integer data type)

ratings = 4.9 (float data type)

name = ‘Bond’ (string data type)

is_done = True (boolean data type)

For example:

price = 10

Taking inputs from users in Python

Python has an in-built function called ‘input’ to take input from the user.

eg: name = input(‘What is your name?’)

Here input function will ask the user ‘What is your name?’ and the user has to enter his/her name. The name will get stored in the ‘name’ variable which we can call further to show the name. Let’s create a simple application in python that ask the user his/her name and greets him with birthday wishes.

name = input('enter your name')
print('Happy birthday' + name)

Here we concatenate the hardcoded string ‘Happy birthday’ with the variable ‘name’ in which the user input is stored. So if you run this code it will ask you about the name. Let’s suppose you entered ‘James’ it will show the output as ‘Happy birthday James

Let’s create another application in Python that will ask the user his birth age and will return his age as output:

birth_year = input('Enter your birth age')
age = 2023 - int('birth_year')

Here we are taking the user’s birth year as input and storing it in the ‘birth_year’ variable. In the second line, we are subtracting the user-given input from 2023 which is the current year and storing the result in the ‘age’ variable, and finally printing it. You can see we have used the ‘int’ in the second line. Why?. Actually what the user gives as input comes in the ‘string’ data type which cannot be used for mathematical calculation. That is why we have typecasted it from ‘string’ to ‘int’ type.

Working with strings

Writing a particular thing from a string. We can show a particular part as output from a given string as shown below.

name = 'Bond James Bond'

#Output will the 1st letter of the string: 'B'

#Output will be the last letter: 'd'

#Output: Bon

#Output:  Bond James Bond

#Output:  Bon

#Output: Bond James Bond

Finding a particular character. Sometimes we need to find a particular character from a string. This can be easily done with the Python in-built function ‘find’. This function returns the position of that character in the string. See the example below:

name = 'Python Basics'


Functions in Python

A function in a programming language is a piece of code that is written once for a certain specific task but can be used multiple times. Suppose you want to add the functionality of greeting a user by taking his/her name at different locations of your application.

No need to write the same complex code again and again at different locations. You just need to create a function for it and to call the function where you need this functionality. See the examples below:

def greet_user():
    name = input('Enter your name ')
    print('Hi', name)
    print('Welcome aboard')

Output:On running the above code, it will ask ‘Enter you name’
Let you enter ‘James’ as the name, then it will show the output as:
Hi James
Welcome Aboard

As can be seen above, we have defined a fun named ‘greet_user’ and have some parameters inside it that we want as output. After taking a two-line gap (which is recommended), we are calling the function to use it.

Function with return value: Return Functions

def square(num):
    return num*num


If Else statement in Python

If else statements are used in a programming language to perform a task when certain conditions are met, otherwise another task.

This can be understood from our daily life examples.

if ‘Morning’, you have to say ‘Good Morning!’. If ‘Afternoon’, you have to say ‘Good Afternoon!’. And if it’s ‘Evening’, you have to say ‘Good Evening!’. Here ‘Morning’ ‘Afternoon’ and ‘Evening’ are conditions that need to be met and ‘Good Morning!’ ‘Good Afternoon!’ and ‘Good Evening!’ are statements that will perform on the fulfillment of the conditions.

The elif keyword is Python’s way of saying “if the previous conditions were not true, then try this condition”. and the else keyword catches anything which isn’t caught by the preceding conditions.

a = 50
b = 40
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
  print("a is greater than b")

In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print that “a is greater than b”. You can also have an else without the elif like below:

a = 50
b = 40
if b > a:
  print("b is greater than a")
  print("b is not greater than a")

For and While loops in Python

Loops are an important construct not just in Python but in all programming languages. In a loop structure, the program first checks for a condition. If this condition is true, some piece of code is run. This code will keep on running unless the condition becomes invalid.

For loop is used to iterate over elements of a sequence. It is often used when you have a piece of code that you want to repeat n number of times. See the below code:

for x in range(6):
Output:0 1 2 3 4 5

In the above code, we have used the range function of Python which will iterate or print the statement until the extreme range is met, ie., 6

While Loop is used to repeat a block of code. Instead of running the code block once, It executes the code block multiple times until a certain condition is met. See the below code:

i = 1
while i < 6:
  i += 1
Output:1 2 3 4 5