Introduction To Python
Introduction To Python
Windows: There are many interpreters available freely to run Python scripts like IDLE
(Integrated Development Environment), which comes bundled with the Python
software downloaded from http://python.org/.
Linux: Python comes preinstalled with popular Linux distros such as Ubuntu and
Fedora. To check which version of Python you’re running, type “python” in the
terminal emulator. The interpreter should start and print the version number.
macOS: Generally, Python 2.7 comes bundled with macOS. You’ll have to manually
install Python 3 from http://python.org/.
Python
1
# Script Begins
Output
GeeksQuiz
Line 2: [print(“GeeksQuiz”)]
To print something on the console, print() function is used. This function also adds a
newline after our message is printed(unlike in C). Note that in Python 2, “print” is not a
function but a keyword and therefore can be used without parentheses. However, in
Python 3, it is a function and must be invoked with parentheses.
Python Features
Interpreted
There are no separate compilation and execution steps like C and C++.
Directly run the program from the source code.
Internally, Python converts the source code into an intermediate form called
bytecodes which is then translated into native language of specific computer to run it.
No need to worry about linking and loading with libraries, etc.
Platform Independent
Python programs can be developed and executed on multiple operating system
platforms.
Python can be used on Linux, Windows, Macintosh, Solaris and many more.
Simple
Closer to English language;Easy to Learn
More emphasis on the solution to the problem rather than the syntax
Embeddable
Python can be used within C/C++ program to give scripting capabilities for the
program’s users.
Robust:
Exceptional handling features
Memory management techniques in built
Python vs JAVA
Python Java
Dynamically Typed Statically Typed
No need to declare anything. An All variable names (along with their types)
assignment statement binds a must be explicitly declared. Attempting to
name to an object, and the object assign an object of the wrong type to a
can be of any type. variable name triggers a type exception.
No type casting is required when Type casting is required when using
using container objects container objects.
Java Code
Java
1
public class HelloWorld
2
{
3
bli t ti id i (St i [] )
Output
Hello, world!
Python Code
Python
1
print("Hello, world!")
Output
Hello, world!
Python Interface
Softwares making use of Python
Python has been successfully embedded in a number of software products as a
scripting language.
1. GNU Debugger uses Python as a pretty printer to show complex structures such as
C++ containers.
2. Python has also been used in artificial intelligence
3. Python is often used for natural language processing tasks.
Current Applications of Python
1. A number of Linux distributions use installers written in Python example in Ubuntu we
have the Ubiquity
2. Python has seen extensive use in the information security industry, including in
exploit development.
3. Raspberry Pi– single board computer uses Python as its principal user-programming
language.
4. Python is now being used Game Development areas also.
Pros:
1. Ease of use
2. Multi-paradigm Approach
Cons:
1. Slow speed of execution compared to C,C++
2. Absence from mobile computing and browsers
3. For the C,C++ programmers switching to python can be irritating as the language
requires proper indentation of code. Certain variable names commonly used like sum
are functions in python. So C, C++ programmers have to look out for these.
Industrial Importance
Most of the companies are now looking for candidates who know about Python
Programming. Those having the knowledge of python may have more chances of
impressing the interviewing panel. So I would suggest that beginners should start
learning python and excel in it.
Applications of Python
1. GUI based desktop applications
2. Graphic design, image processing applications, Games, and Scientific/ computational
Applications
3. Web frameworks and applications
4. Enterprise and Business applications
5. Operating Systems
6. Education
7. Database Access
8. Language Development
9. Prototyping
10. Software Development
List of Companies Using Python
1. Google(Components of Google spider and Search Engine)
2. Yahoo(Maps)
3. YouTube
4. Mozilla
5. Dropbox
6. Microsoft
7. Cisco
8. Spotify
9. Quora
So before moving on further.. let’s do the most popular ‘HelloWorld’ tradition and hence
compare Python’s Syntax with C, C++, Java ( I have taken these 3 because they are
most famous and mostly used languages). If you want to explore more programming
examples then explore GeeksforGeeks Python course, in this course you can get all the
paid topics of Python without any cost.
Python
1
# Python code for "Hello World"
2
# nothing else to type...see how simple is the syntax.
Output
Hello World
Note: Please note that Python for its scope doesn’t depend on the braces ( { } ), instead
it uses indentation for its scope. Now moving on further
Lets start our basics of Python. I will be covering the basics in some small sections. Just
go through them and trust me you’ll learn the basics of Python very easily.
Introduction and Setup
If you are on Windows OS download Python by Clicking here and now install from the
setup and in the start menu type IDLE.IDLE, you can think it as an Python’s IDE to run
the Python Scripts. It will look somehow this:
If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes
preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go. It will
look like this :
The ” >>> ” represents the python shell and its ready to take python commands
and code.
Python
1
# Python program to declare variables
2
myNumber = 3
3
print(myNumber)
Output
3
4.5
helloworld
See, how simple is it, just create a variable and assign it any value you want and then use the print function
to print it. Python have 4 types of built in Data Structures namely List, Dictionary, Tuple and Set. List It is
the most basic Data Structure in python. List is a mutable data structure i.e items can be added to list later
after the list creation. It's like you are going to shop at the local market and made a list of some items and
later on you can add more and more items to the list. append() function is used to add data to the list.
Python
Python
1
# creates a empty list
2
nums = []
4
# appending data in list
Output
[21, 40.5, 'String']
Python Comments
# is used for single line comment in Python
""" this is a comment """ is used for multi line comments
Python
1
# Python program to illustrate
2
# getting input from user
3
name = input("Enter your name: ")
Output
Enter your name: Geeksforgeeks
hello Geeksforgeeks
Python
Output
1
# Python3
Enter num1: 4 program to get input from user
Enter num2: 8
Product2 is: 32
1
# Python program to illustrate
2
# selection statement
3
num1 = 34
4
if(num1>12):
Output
Num1 is good
Functions in Python
You can think of functions like a bunch of code that is intended to do a particular task in the whole Python
script. Python used the keyword 'def' to define a function.
Syntax:def function-name(arguments):
#function bodyPython
Python
1
# Python program to illustrate
2
# functions
3
def hello():
Output
hello
hello again
hello
hello again
Now as we know any program starts from a ‘main’ function…lets create a main function
like in many other programming languages.
Python
1
# Python program to illustrate
2
# function with main
3
def getInteger():
4
result = int(input("Enter integer: "))
5
return result
7
def Main():
8
Output
Started
Enter integer: 3
3
As the name suggests it calls repeating things again and again. We will use the most popular 'for' loop here.
Python
1
# Python program to illustrate
2
# a simple for loop
Output
0
1
2
3
4
Modules in Python
Python has a very rich module library that has several functions to do many tasks. You
can read more about Python’s standard library by Clicking here ‘import’ keyword is used
to import a particular module into your python code. For instance consider the following
program.
Python
1
import math
2
3
def Main():
4
num = -85
Output
85.0