Python

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Python Introduction

• Python is a general-purpose interpreted,


interactive, object-oriented, and high-level
programming language.
• It was created by Guido van Rossum during
1985- 1990. Like Perl, Python source code is
also available under the GNU General Public
License (GPL).
Why to Learn Python?
• Python is a high-level, interpreted, interactive
and object-oriented scripting language.
Python is designed to be highly readable. It
uses English keywords frequently where as
other languages use punctuation, and it has
fewer syntactical constructions than other
languages.
Why to Learn Python?
• Python is Interpreted − Python is processed at runtime by the interpreter.
You do not need to compile your program before executing it. This is similar
to PERL and PHP.

• Python is Interactive − You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.

• Python is Object-Oriented − Python supports Object-Oriented style or


technique of programming that encapsulates code within objects.

• Python is a Beginner's Language − Python is a great language for the


beginner-level programmers and supports the development of a wide range
of applications from simple text processing to WWW browsers to games.
Applications of Python
• Easy-to-learn − Python has few keywords, simple structure, and a
clearly defined syntax. This allows the student to pick up the
language quickly.

• Easy-to-read − Python code is more clearly defined and visible to the


eyes.

• Easy-to-maintain − Python's source code is fairly easy-to-maintain.

• A broad standard library − Python's bulk of the library is very


portable and cross-platform compatible on UNIX, Windows, and
Macintosh.
Applications of Python
• Interactive Mode − Python has support for an interactive mode
which allows interactive testing and debugging of snippets of code.

• Portable − Python can run on a wide variety of hardware platforms


and has the same interface on all platforms.

• Extendable − You can add low-level modules to the Python


interpreter. These modules enable programmers to add to or
customize their tools to be more efficient.

• Databases − Python provides interfaces to all major commercial


databases.
What can Python do?

• Python can be used on a server to create web


applications.
• Python can be used alongside software to create
workflows.
• Python can connect to database systems. It can also
read and modify files.
• Python can be used to handle big data and perform
complex mathematics.
• Python can be used for rapid prototyping, or for
production-ready software development.
Python data types, objects and other
statements
• Identifiers:
A Python identifier is a name used to identify a
variable, function, class, module or other object.
An identifier starts with a letter A to Z or a to z or
an underscore (_) followed by zero or more
letters, underscores and digits (0 to 9).
• Reserved Words:
And exec not assert finally or break
for pass class
Python data types, objects and other
statements
• Quotation in Python:
Python accepts single ('), double (")
word = 'word'
sentence = "This is a sentence.“
• Comments in Python:
A hash sign (#) that is not inside a string
literal begins a comment
# First comment
print "Hello, Python!" # second comment
Python data types, objects and other
statements
• Waiting for the User:

raw_input("\n\nPress the enter key to exit.")


Python data types, objects and other
statements
• Variables:
Variables are nothing but reserved memory locations to store
values. This means that when you create a variable you reserve
some space in memory.

counter = 100 # An integer assignment


miles = 1000.0 # A floating point
name = "John" # A string

print counter
print miles
print name
Python data types, objects and other
statements
• Variables:
Multiple Assignment:

a=b=c=1

a,b,c = 1,2,"john"
Python data types, objects and other
statements
• Standard Data Types:
Python has five standard data types :
– Numbers
– String
– List
– Tuple
– Dictionary
Python data types, objects and other
statements
• Standard Data Types:
Numbers:
Python supports four different numerical types −
– int (signed integers)
– long (long integers, they can also be represented
in octal and hexadecimal)
– float (floating point real values)
– complex (complex numbers)
Python data types, objects and other
statements
• Standard Data Types:
Numbers:
Here are some examples of numbers −

intlong float complex


10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j
-0490 535633629843L -90. -.6545+0J
-0x260-052318172735L -32.54e100 3e+26J
0x69 -4721885298529L 70.2-E12 4.53e-7j
Python data types, objects and other
statements
• Standard Data Types:
Strings: Python allows for either pairs of single or double quotes. Subsets of strings can be taken
using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working
their way from -1 at the end.

str = 'Hello World!'


print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
Python data types, objects and other
statements
• Standard Data Types:
Lists: A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are
similar to arrays in C. The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting
at 0 in the beginning of the list and working their way to end -1

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


tinylist = [123, 'john']

print list # Prints complete list


print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
Python data types, objects and other
statements
• Standard Data Types:
Tuples: Similar to the list. The main differences between lists and tuples are: Lists are enclosed in brackets ( [
] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated.

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )


tinytuple = (123, 'john')

print tuple # Prints the complete tuple


print tuple[0] # Prints first element of the tuple
print tuple[1:3] # Prints elements of the tuple starting from 2nd till 3rd
print tuple[2:] # Prints elements of the tuple starting from 3rd element
print tinytuple * 2 # Prints the contents of the tuple twice
print tuple + tinytuple # Prints concatenated tuples
Python data types, objects and other
statements
• Standard Data Types:
Dictionary: Kind of hash table. They keep the data in the form of key-value pairs. dictionary key can be almost
any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

print dict['one'] # Prints value for 'one' key


print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values

You might also like