Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
Python Programming Language is a high-level and interpreted programming language which was created by Guido Van Rossum in 1989 and released in 1991.
Python being a great general purpose and high level language, can be used to create Desktop GUI applications, web applications and web frameworks.
Python is both a scripting language and programming language. A scripting language works on the basis of automating a repeated task such as the execution of a procedure or program.
For those of you familiar with Java or C++, Python will break the mold you have built for a typical programming language. Prepare to fall in love, with Python!
In this blog, we will learn Python Programming language in the following sequence:
To get in-depth knowledge on Python Programming language along with its various applications, you can enroll now for the best Python course online training with 24/7 support and lifetime access.
Python is a high-level dynamic programming language. It is quite easy to learn and provides powerful typing. Python code has a very ‘natural’ style to it, in that it is easy to read and understand (thanks to the lack of semicolons and braces). Python programming language runs on any platform, ranging from Windows to Linux to Macintosh, Solaris etc.
Yes. Python is an open-source programming language that is freely available for everyone. It is also backed by a growing ecosystem of open-source packages and libraries. Anybody interested to work on Python can download and install it for free, from there official website: https://www.python.org/
Python is an easy language to learn and it should ideally be your first programming language because you will quickly learn how to think like a programmer. The simplicity of Python is what it makes so popular. The following gives a highlight of its aesthetics:
Wait! Python can do more.
It is a very popular language in multiple domains like Automation, Big Data, AI etc. You can refer to this entire blog on top 10 reasons to learn python.
You will also be impressed as Python is used for Cyber security by the vast multitude of companies around the globe.
You may go through the webinar recording of Python Programming Language where our Python training expert has explained the topics in a detailed manner with examples that will help you to understand Python Programming language better.
Let us now move on to installing Python on a Windows systems.
Figure: Downloading Python Programming Language
2. Download and install PyCharm IDE.
Figure: Downloading PyCharm
PyCharm is an Integrated Development Environment (IDE) used in computer programming, specifically for the Python programming language. It provides code analysis, a graphical debugger, an integrated unit tester, integration with version control systems (VCSes), and supports web development with Django.
The following are the five fundamentals required to master Python:
Figure: Python Programming Language- Fundamentals
All data values in Python are represented by objects and each object or value has a datatype.
Figure: Python Programming Language – Datatype Features
There are eight native datatypes in Python.
The following image will give a description for the same.
Figure: Python Programming Language – Native Datatypes
Let us look at how to implement these data types in Python.
#Boolean number = [1,2,3,4,5] boolean = 3 in number print(boolean) #Numbers num1 = 5**3 num2 = 32//3 num3 = 32/3 print('num1 is',num1) print('num2 is',num2) print('num3 is',num3) #Strings str1 = "Welcome" str2 = " to Edureka's Python Programming Blog" str3 = str1 + str2 print('str3 is',str3) print(str3[0:10]) print(str3[-5:]) print(str3[:-5]) #Lists countries = ['India', 'Australia', 'United States', 'Canada', 'Singapore'] print(len(countries)) print(countries) countries.append('Brazil') print(countries) countries.insert(2, 'United Kingdom') print(countries) #Tuples sports_tuple = ('Cricket', 'Basketball', 'Football') sports_list = list(sports_tuple) sports_list.append('Baseball') print(sports_list) print(sports_tuple) #Dictionary #Indian Government Government = {'Legislature':'Parliament', 'Executive':'PM & Cabinet', 'Judiciary':'Supreme Court'} print('Indian Government has ',Government) #Modifying for USA Government['Legislature']='Congress' Government['Executive']='President & Cabinet' print('USA Government has ',Government)
The output of the above code is as follows:
True num1 is 125 num2 is 10 num3 is 10.666666666666666 str3 is Welcome to Edureka's Python Programming Blog Welcome to Blog Welcome to Edureka's Python Programming 5 ['India', 'Australia', 'United States', 'Canada', 'Singapore'] ['India', 'Australia', 'United States', 'Canada', 'Singapore', 'Brazil'] ['India', 'Australia', 'United Kingdom', 'United States', 'Canada', 'Singapore', 'Brazil'] ['Cricket', 'Basketball', 'Football', 'Baseball'] ('Cricket', 'Basketball', 'Football') Indian Government has {'Legislature': 'Parliament', 'Judiciary': 'Supreme Court', 'Executive': 'PM & Cabinet'} USA Government has {'Legislature': 'Congress', 'Judiciary': 'Supreme Court', 'Executive': 'President & Cabinet'}
Flow Control lets us define a flow in executing our programs. To mimic the real world, you need to transform real world situations into your program. For this you need to control the execution of your program statements using Flow Controls.
Figure: Python Programming Language- Flow Control
There are six basic flow controls used in Python programming:
The Python compound statement ’if’ lets you conditionally execute blocks of statements.
Syntax of If statement:
if expression: statement (s) elif expression: statement (s) elif expression: statement (s) ... else: statement (s)
Figure: Python Programming Language – If – Facebook Login Example
The above image explains the use of ‘if’ statement using an example of Facebook login.
Let us now look at how Facebook would use the If statement.
password = facebook_hash(input_password) if password == hash_password print('Login successful.') else print('Login failed. Incorrect password.')
The above code just gives a high level implementation of If statement in the Facebook login example used. Facebook_hash() function takes the input_password as a parameter and compares it with the hash value stored for that particular user.
The for statement supports repeated execution of a statement or block of statements that is controlled by an iterable expression.
Syntax of For statement:
for target in iterable: statement (s)
Figure: Python Programming Language- For – Facebook Friends Example
The ‘for’ statement can be understood from the above example.
Let us now look at a sample program in Python to demonstrate the For statement.
travelling = input("Are you travelling? Yes or No:") while travelling == 'yes': num = int(input("Enter the number of people travelling:")) for num in range(1,num+1): name = input("Enter Details Name:") age = input("Age:") sex = input("Male or Female:") print("Details Stored ",name) print(age) print(sex) print("Thank you!") travelling = input("Are you travelling? Yes or No:") print("Please come back again.")
The output is as below:
Are you travelling? Yes or No:Yes Enter the number of people travelling:1 Enter Details Name:Harry Age:20 Male or Female:Male Details Stored Harry 20 Male Thank you Are you travelling? Yes or No:No Please come back again.
The while statement in Python programming supports repeated execution of a statement or block of statements that is controlled by a conditional expression.
Syntax of While statement:
while expression: statement (s)
Figure: Python Programming Language – While – Facebook Newsfeed Example
We will use the above Facebook Newsfeed to understand the use of while loop.
Let us now look at a sample program in Python to demonstrate the While statement.
count = 0 print('Printing numbers from 0 to 9') while (count<10): print('The count is ',count) count = count+1 print('Good Bye')
This program prints numbers from 0 to 9 using the while statement to restrict the loop till it reaches 9. The output is as below:
The count is 0 The count is 1 The count is 2 The count is 3 The count is 4 The count is 5 The count is 6 The count is 7 The count is 8 The count is 9
The break statement is allowed only inside a loop body. When break executes, the loop terminates. If a loop is nested inside other loops, break terminates only the innermost nested loop.
Syntax of Break statement:
while True: x = get_next() y = preprocess(x) if not keep_looking(x, y): break process(x, y)
Figure: Python Programming Language- Break – Alarm And Incoming Call
The ‘break’ flow control statement can be understood from the above example.
Let us now look at a sample program in Python to demonstrate the Break statement.
for letter in 'The Quick Brown Fox. Jumps, Over The Lazy Dog': if letter == '.': break print ('Current Letter :', letter)
This program prints all the letters in a given string. It breaks whenever it encounters a ‘.’ or a full stop. We have done this by using Break statement. The output is as below.
Current Letter : T Current Letter : h Current Letter : e Current Letter : Current Letter : Q Current Letter : u Current Letter : i Current Letter : c Current Letter : k Current Letter : Current Letter : B Current Letter : r Current Letter : o Current Letter : w Current Letter : n Current Letter : Current Letter : F Current Letter : o Current Letter : x
The continue statement is allowed only inside a loop body. When continue executes, the current iteration of the loop body terminates, and execution continues with the next iteration of the loop.
Syntax of Continue statement:
for x in some_container: if not seems_ok(x): continue lowbound, highbound = bounds_to_test() if x<lowbound or x>=highbound: continue if final_check(x): do_processing(x)
Figure: Python Programming Language – Continue – Incoming Call And Alarm Example
Example: The Continue statement can be understood using incoming call and an alarm.
Let us now look at a sample program in Python to demonstrate the Continue statement.
for num in range(10, 21): if num % 5 == 0: print ("Found a multiple of 5") pass num = num + 1 continue print ("Found number: ", num)
This program prints all the numbers except the multiples of 5 from 10 to 20. The output is as follows.
Found a multiple of 5 Found number: 11 Found number: 12 Found number: 13 Found number: 14 Found a multiple of 5 Found number: 16 Found number: 17 Found number: 18 Found number: 19 Found a multiple of 5
The pass statement, which performs no action, can be used as a placeholder when a statement is syntactically required but you have nothing specific to do.
Syntax of Pass statement:
if condition1(x): process1(x) elif x>23 or condition2(x) and x<5: pass elif condition3(x): process3(x) else: process_default(x)
Now let us look at a sample program in Python to demonstrate the Pass statement.
for num in range(10, 21): if num % 5 == 0: print ("Found a multiple of 5: ") pass num++ print ("Found number: ", num)
This program prints the multiples of 5 with a separate sentence. The output is as follows.
Found a multiple of 5: 10 Found number: 11 Found number: 12 Found number: 13 Found number: 14 Found a multiple of 5: 15 Found number: 16 Found number: 17 Found number: 18 Found number: 19 Found a multiple of 5: 20
After learning the above six flow control statements, let us now learn what functions are.
Functions in Python programming, is a group of related statements that performs a specific task. Functions make our program more organized and help in code re-usability.
Figure: Python Programming Language – Understanding Functions
Figure: Python Programming Language – Demonstrating The Uses Of Functions
The code used in the above example is as below:
# Defining a function to reverse a string def reverse_a_string(): # Reading input from console a_string = input("Enter a string") new_strings = [] # Storing length of input string index = len(a_string) # Reversing the string using while loop while index: index -= 1 new_strings.append(a_string[index]) #Printing the reversed string print(''.join(new_strings)) reverse_a_string()
We have thus shown the power of using functions in Python.
File Handling refers to those operations that are used to read or write a file.
To perform file handling, we need to perform these steps:
Figure: Python Programming Language- File Handling In Python
Example program:
file = open("C:/Users/Edureka/Hello.txt", "r") for line in file: print (line)
The output is as below:
One Two Three
Example program:
with open("C:/Users/Edureka/Writing_Into_File.txt", "w") as f f.write("First Line ") f.write("Second Line ") file = open("D:/Writing_Into_File.txt", "r") for line in file: print (line)
The output is as below:
First Line Second Line
Example program:
file = open("C:/Users/Edureka/Writing_Into_File.txt", "r") print(file.read(5)) print(file.read(4)) print(file.read())
The output is as below:
First Line Second Line
file = open("C:/Users/Edureka/Hello.txt", "r") text = file.readlines() print(text) file.close()
The output is as below:
['One ', 'Two ', 'Three']
Python is an object oriented programming language. Object is simply a collection of data (variables) and methods (functions) that act on those data. Class is a blueprint for the object.
We define a class using the keyword “Class”. The first string is called docstring and has a brief description about the class.
class MyNewClass: '''This is a docstring. I have created a new class''' pass
A Class object can be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call.
ob = MyNewClass
We have thus learnt how to create an object from a given class.
So this concludes our Python Programming blog. I hope you enjoyed reading this blog and found it informative. By now, you must have acquired a sound understanding of what Python Programming Language is. Now go ahead and practice all the examples.
Got a question for us? Please mention it in the comments section of “Python Programming Language” blog and we will get back to you at the earliest.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 30th November,2024 30th November SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 28th December,2024 28th December SAT&SUN (Weekend Batch) | View Details |
edureka.co
Thanks alot..
No more words to say….
It was such a great article. i really acquire a lot of new knowledge about python through this article. I would remain greatful to you if you keep on updating new block about python in the future.
thank you
Very useful information for the absolute beginners of python programming . Really worth reading. Thanks for sharing.