Download presentation
Presentation is loading. Please wait.
Published byEdwina Lane Modified over 6 years ago
1
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during Like Perl, Python source code is also available under the GNU General Public License (GPL).
2
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.
3
Features of Python 1. Simple 2. Easy to Learn 3. Free and Open Source
4. High Level Language 5. Portable 6. Interpreted 7. Object oriented 8. Extensible 9. Embeddable 10. Extensive Libraries
4
Statements and Syntax Some rules and certain symbols are used with regard to statements in Python: 1. Hash mark ( # ) indicates Python comments 2. NEWLINE ( \n ) is the standard line separator (one statement per line) 3. Backslash ( \ ) continues a line 4. Semicolon ( ; ) joins two statements on a line 5. Colon ( : ) separates a header line from its suite 6. Statements (code blocks) grouped as suites 7. Suites delimited via indentation 8. Python files organized as "modules"
5
Standard Types The basic standard data types that python supports are:
1. Numbers (four separate sub-types) 2. Regular or "Plain" Integer 3. Long Integer 4. Floating Point Real Number 5. Complex Number 6. String 7. List 8. Tuple 9. Dictionary
6
Lines and Indentation Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced. if True: print "True" else: print "False"
7
Quotation in Python Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string.
8
Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. counter = 100 # An integer assignment miles = # A floating point name = "John" # A string print counter print miles print name
9
Python Lists Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type. 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. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator. For example −
10
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 This produce the following result − ['abcd', 786, 2.23, 'john', ] abcd [786, 2.23] [2.23, 'john', ] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', , 123, 'john']
11
Python Tuples A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses. 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. Tuples can be thought of as read-only lists. For example −
12
tuple = ( 'abcd', 786 , 2.23, 'john', ) tinytuple = (123, 'john') print tuple # Prints complete list print tuple[0] # Prints first element of the list print tuple[1:3] # Prints elements starting from 2nd till 3rd print tuple[2:] # Prints elements starting from 3rd element print tinytuple * 2 # Prints list two times print tuple + tinytuple # Prints concatenated lists This produce the following result − ('abcd', 786, 2.23, 'john', ) abcd (786, 2.23) (2.23, 'john', ) (123, 'john', 123, 'john') ('abcd', 786, 2.23, 'john', , 123, 'john')
13
Python Dictionary Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A 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. Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). For example −
14
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 This produce the following result − This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']
15
Types of Operator Python language supports the following types of operators. Arithmetic Operators Comparison (Relational) Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators Identity Operators
16
Python Decision Making
If statement if expression: statement(s) If..else statement else: Nested If statement In a nested if construct, you can have an if...elif...else construct inside another if...elif...else construct.
17
if expression1: statement(s) if expression2: elif expression3: else elif expression4: else:
18
Python Loops A loop statement allows us to execute a statement or group of statements multiple times. while Loop count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"
19
for Loop for iterating_var in sequence: statements(s) for num in range(10,20): #to iterate between 10 to 20 for i in range(2,num): #to iterate on the factors of the number
20
nested loops Syntax: for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s)
21
Python Date & Time A Python program can handle date and time in several ways. Converting between date formats is a common chore for computers. Python's time and calendar modules help track dates and times. What is Tick? Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch). There is a popular time module available in Python which provides functions for working with times, and for converting between representations. The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).
22
import time; # This is required to include time module.
ticks = time.time() print "Number of ticks since 12:00am, January 1, 1970:", ticks
23
Python Functions def printme( str ):
"This prints a passed string into this function" print str return Calling a Function def printme( str ): # function definition printme("I'm first call to user defined function!") printme("Again second call to the same function")
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.