Date post: | 17-Aug-2015 |
Category: |
Education |
Upload: | hemant-kumar-tiwari |
View: | 91 times |
Download: | 1 times |
PythonOverview
Definition
Python is a widely used general-purpose, high-level programming language that lets you work quickly and integrate systems more effectively.
Philosophy
● Beautiful is better than Ugly● Explicit is better than Implicit● Simple is better than Complex● Complex is better than Complicated● Readability counts
Command to check version
hemant@ubuntu:~$ pythonPython 2.7.3 (default, Apr 10 2013, 06:20:15) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.>>> |
Arithmetic Operators allowed
Arithmetic operators we can use:+ - * / Addition, Subtraction/Negation, Multiplication, Division
% Modulus, a.k.a. remainder
** Exponentiation
“+” and “-” have equal precedence“*”, “/”, “%” and “**” have equal precedence
“*”, “/”, “%” and “**” have a higher precedence than “+” and “-”
Operators Precedence
Operations Examples
Parentheses can be used to force a certain order of evaluation.(1 + 3) * 4 is 16Division results as an integer.35 / 5 is 7, 84 / 10 is 8The % operator computes the remainder from a division of integers.35 % 5 is 0, 8 % 3 is 2
Print command Examples
Print : It print output to console.Syntax:print "Python"print Expressionprint 3,6,”python”total_session = 2print "You have", total_session - 1, " session left"Output : You have 1 session left
Command to Input Data
>>> name1 = input("What is your name? ")What is your name? "hemant">>> print name1hemant
>>> age = input("How old are you? ")How old are you? 10>>> print "Your age is", ageYour age is 10
Command to Manage “Loop’s”
Loop: Repeats a statements or set of statements as group.
Example:for name in studentslist: print name
Note : We indent the statements to be repeated with tabs or spaces.Example :>>> studentlist = ["Eric", "John", "Michael"]>>> for name in studentlist:... print name
Loop’s
Loop’s for a define ‘Range’
The range function specifies a range of integers:range(start, stop) - the integers between start (inclusive) and stop (exclusive)It can also accept a third value specifying the change between values.range(start, stop, step)
Example:>>> for x in range(5, 0, -1):... print x
>>> for x in range(0, 5, 1):... print x
Example for ‘Range’
Example for Whilewhile loop: Executes a group of statements as long as a condition is True.Syntax:while condition: statements
Example:number = 10while number < 40: print number number = number * 2
Conditional Statement
if statement: Executes a group of statements in true condition.if condition: statementsif/else statement: If block execute in true condition otherwise else get executed.if condition: statementselse: statements
Logical Operator
Relational Operators:== , != , < , >, <= , >=
Logical Operator and, or, not
StringString: A sequence of text characters in a program. it start and end with quotation mark " or apostrophe ' characters.
Examples: "hello"
A string can represent characters by preceding them with a backslash.\t tab character\n new line character\" quotation mark character\\ backslash character
String - Operatorslen(String) - Number of characters in a string (Including spaces)str.lower(String)- Lowercase version of a stringstr.upper(String) - Uppercase version of a string
Example:name = "Python Django Delhi"length = len(name)big_name = str.upper(name)print big_name, "has", length, "characters"
Command to take Input
raw_input : Reads a string of text from user input.
Example:n1 = raw_input("Enter first number? ")n2 = raw_input("Enter second number? ")print n1 + n2
String-Indexes /Text ProcessingCharacters in a string can be access through indexes starting at 0. Accessing an individual character of a string:
Example 1:name = "Python"print name, "starts with", name[0]Output: Python starts with P
Example 2:for c in "Python": print c
String to Numbers Conversion
ord(text) - Converts a string into a number.Example: ord("A") is 65, ord("a") is 97
chr(number) - Converts a number into a string.Example: chr(98) is "b"
Command for File HandlingReading the entire contents of a file:variableName = open("filename").read()Example:>>> f1 = open("/home/django/svn_install.txt").read()>>> print f1
Line by Line>>> for line in open("/home/django/svn_install.txt").readlines():... print line... print "-----------"
File Handling [Continue]Writing Data in file
>>> handle = open("/home/django/sample.txt", "w")>>> handle.write("sample data text")>>> handle.close()
Reading data from file
>>> f1 = open("/home/django/sample.txt").read()>>> print f1sample data text
There are several popular implementations of the Python programming language on different back-ends.
Jython Compiles into Java byte code, which can then be executed by every Java Virtual Machine implementation. This also enables the use of Java class library functions from the Python program.
IronPython follows a similar approach in order to run Python programs on the .NET Common Language Runtime.
Cython and Pyrex compile to C.
PyPy is a Python interpreter and just-in-time compiler. PyPy focuses on speed, efficiency and compatibility with the original CPython interpreter.
Implementations
https://wiki.python.org/moin/OrganizationsUsingPython
Organizations that use Python
Thank You.