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
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.
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
You can learn more about Indentation in Python here.
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:
- 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.
- 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
floatdata type to store floating-point numbers.
- Booleans: A boolean value is either ‘True’ or
'False'. In Python, you can use the ‘bool’ data type to store boolean values.
- Strings: A string is a series of characters, such as a word or a phrase. In Python, you can use the
strdata type to store strings.
- 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
listdata type to store lists.
- 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
tupledata type to store tuples.
- Sets: A set is an unordered collection of unique values. In Python, you can use the
setdata type to store sets.
- Dictionaries: A dictionary is a collection of key-value pairs. In Python, you can use the
dictdata 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") #or print('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)
price = 10 print(price)
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') print(age)
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' print(name) #Output will the 1st letter of the string: 'B' print(name[-1]) #Output will be the last letter: 'd' print(name[0:3]) #Output: Bon print(name[0:]) #Output: Bond James Bond print(name[:3]) #Output: Bon print(name[:]) #Output: Bond James Bond
Finding the number of strings. We can find the number of entities in a string in Python using the ‘len’ function. This also calculates the spaces. Example:
name = 'Bond James Bond' print(len(name))
Converting a string into upper/lower case. In Python, we can easily convert a given string into uppercase or lowercase. See the example below:
name = 'Python Tutorial' print(name.upper()) print(name.lower())
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' print(name.find('h')) print(name.find('on'))
Read about Python find in list
Replacing characters in a string in Python. Python has an in-built function called ‘replace’ to replace certain character(s) from a string with the provided character(s). See the example below:
Boolean Check. Boolean Check is used to know whether a given condition is correct or wrong. Its output is either True or False. See the below example:
title = 'Learning Python' print('Learning' in title) #Output: True (Because Learning string exist) print('Gaming' in title) #Output: False (Gaming string doesn't exist)
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') gret_user()
Let you enter ‘James’ as the name, then it will show the output as:
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 print(square(3))
Here we have defined a square function that will take a numerical value as a parameter and will return the square of the number.
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") else: 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") else: print("b is not greater than a")
You can learn more about Python If Else Statements here.
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): print(x)
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: print(i) i += 1
List, Set, Tuple, and Dictionary in Python
Python Reverse List items
Python Round() Function
Python Multiline comment
Power in Python | Python pow()
Python range() Function
Square Root in Python
Python for i in range
22 thoughts on “Python Basics: Basics of Python Programming”
Pingback: Pillow Python: What it is and How to use it? – Programming Tutorial
Pingback: Python XML and ElementTree Module – Programming Tutorial
Pingback: Paramiko in Python – Programming Tutorial
Pingback: Best Python IDE | IDE and Code Editors for Python – Programming Tutorial
Pingback: ChatGPT Python: How to use ChatGPT in Python Project – Programming Tutorial
Pingback: Square Root in Python – Programming Tutorial
Pingback: Python range() Function | Range Python – Programming Tutorial
Pingback: Python for i in range | Python For Loop – Learn to Code
Pingback: Multiline comment python | Python Multiline comment
Pingback: Python Reverse List items
Pingback: Best Budget Laptop for Programming
Pingback: Python Round() Function
Pingback: Power in Python | Python pow()
Pingback: List, Set, Tuple, and Dictionary Data Structures in Python
Pingback: Python if not | Not operator with if
Pingback: Python OS Module | OS Module in Python
Pingback: Python Split | String split() in Python
Pingback: SQL | SQL Tutorial with Examples
Pingback: modulenotfounderror no module named ‘tkinter’
Pingback: Cisco NetAcad Learning
Pingback: NumPy Tutorial | Basic of NumPy Arrays