Introduction to Python

Python is a widely used high-level programming language created by Guido van Rossum and first released in 1991.

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).

var1 = 1

var2 = 10

Keywords 

These are reserved words and you cannot use them as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.

 

Examples - 

 

and , exec, for, if etc.

Declaring 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.

 

 

var1 = 1
var2 = 10
name = "John"

Standard Data Types 

Python has five standard data types −

  • Numbers

  • String : identified using quotation marks

  • List : items separated by commas enclosed in brackets. Size can be changed.

  • Tuple : similar to list enclosed in parentheses. Size cannot be further updated.

  • Dictionary

    dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

Decision Making

Loops

  • While 
  • For
  • Do while

Lists

In Python list can be written as a list of comma-separated values (items) between square brackets

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

Dictionary

Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']

Functions

A function is a block of organized, reusable code that is used to perform a single, related action.

def printme( str ):
   "This prints a passed string into this function"
   print str
   return
def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]

Example -

Tuples

A tuple is a sequence of immutable Python objects.

#!/usr/bin/python

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );

print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]

deck

By haritha28

deck

  • 1,082