Python
WHAT IS PYTHON
➢Multi-purpose(Web, GUI, Scripting etc)
➢Object oriented
➢Interpreted
➢Strongly typed and Dynamically typed
➢Focus on readability and productivity
Python Features
FEATURES
• Everything is an object
• Interactive shell
• Strong Introspection
• Cross Platform
• A broad standard library
• Cpython, Jython, Ironpython, PyPy
Contd.,
Who uses Python
Python Implementations
RELEASES
• Created in 1989 by Guido Van Rossum
• Python 1.0 released in 1994
• Python 2.0 released in 2000
• Python 3.0 released in 2008
Python 2 Vs Python 3
WHAT IS NEW IN PYTHON 3.0
• Python 3.x's integer division behavior in Python 2 – add from __future__ import division
• Use of parenthesis () with print function is now mandatory
• The print() function inserts a new line at the end, by default.
• In Python 2, it can be suppressed by putting ',' at the end
• In Python 3, "end =' '" appends space instead of newline.
• In Python 3, raw_input() function is deprecated.
• In Python 2, the result of division of two integers is rounded to the nearest integer
• Python 3 evaluates 3 / 2 as 1.5 by default
• Python 3 stores strings as Unicode, by default.
CONTD.,
• In Python 2 range() returns a list, and xrange() returns an object
• In Python 3, xrange() has been renamed as range().
• raise IOError, "file error“
• #This is accepted in Python 2 raise IOError("file error") #This is also accepted in Python 2
• raise IOError("file error") #this is the recommended syntax in Python 3
• In Python 3, arguments to exception should be declared with 'as' keyword.
Python Standard library
Python-OS flavors
Significant Whitespace Rules
Prefer Four Spaces
Never mix spaces and tabs
Be consistent on Consecutive lines
Only deviate to improve readability
PYTHON QUICKSTART
➢ Python is an interpreted programming language, this means that as a
developer you write Python (.py) files in a text editor and then put those
files into the python interpreter to be executed.
➢ The way to run a python file is like this on the command line:
➢ C:\Users\Your Name>python helloworld.py
➢ Where "helloworld.py" is the name of your python file.
FIRST PYTHON FILE: HELLO WORLD
➢helloworld.py
➢print("Hello, World!")
➢Simple as that. Save your file. Open your command line, navigate to the directory where you
saved your file, and run:
➢C:\Users\Your Name>python helloworld.py
➢The output should read:
➢Hello, World!
PYTHON COMMENTS
➢ Comments can be used to explain Python code.
➢Comments can be used to make the code more readable.
➢Comments can be used to prevent execution when testing code.
CREATING A COMMENT
➢ Comments starts with a #, and Python will ignore them: Example
➢ #This is a comment
➢ print("Hello, World!")
➢ Comments can be placed at the end of a line, and Python will ignore the rest of the
line:
➢ Example
➢ print("Hello, World!") #This is a comment
➢ A comment does not have to be text that explains the code, it can also be used to
prevent Python from executing code:
➢Example
➢ #print("Hello, World!")
➢ print("Cheers, Mate!")
MULTI LINE COMMENTS CONTD..
➢ Python does not really have a syntax for multi line comments.
➢To add a multiline comment you could insert a # for each line:
➢Example
➢#This is a comment
➢#written in
➢#more than just one line
➢print("Hello, World!")
MULTI LINE COMMENTS
➢ Or, not quite as intended, you can use a multiline string.
➢Since Python will ignore string literals that are not assigned to a variable, you can
add a multiline string (triple quotes) in your code, and place your comment inside it:
➢Example
➢"""
➢This is a comment
➢written in
➢more than just one line
➢"""
➢print("Hello, World!")
PYTHON VARIABLES
➢ Variables are containers for storing data values.
➢ To Creating Variables Python has no command for declaring a variable.
➢A variable is created the moment you first assign a value to it.
➢ Example
➢x=5
➢ y = "John"
➢ print(x)
➢ print(y)
NO DECLARATION
➢ Variables do not need to be declared with any particular type, and can
even change type after they have been set.
➢Example
➢x = 4 # x is of type int
➢x = "Sally" # x is now of type str
➢print(x)
CASTING
➢ If you want to specify the data type of a variable, this can be done with
casting.
➢Example
➢x = str(3) # x will be '3'
➢y = int(3) # y will be 3
➢z = float(3) # z will be 3.0
GET THE TYPE
➢ You can get the data type of a variable with the type() function.
➢Example
➢x = 5
➢y = "John"
➢print(type(x))
➢print(type(y))
SINGLE OR DOUBLE QUOTES?
➢ String variables can be declared either by using single or double
quotes:
➢Example
➢x = "John"
➢# is the same as
➢x = 'John'C
CASE-SENSITIVE
➢ Variable names are case-sensitive.
➢Example
➢This will create two variables:
➢a = 4
➢A = "Sally"
➢#A will not overwrite a
VARIABLE NAMES
➢ A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume). Rules for Python variables:
➢ A variable name must start with a letter or the underscore character
➢ A variable name cannot start with a number
➢ A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
➢ Variable names are case-sensitive (age, Age and AGE are three
different variables)
LEGAL VARIABLE NAMES
➢ myvar = "John"
➢my_var = "John"
➢_my_var = "John"
➢myVar = "John"
➢MYVAR = "John"
➢myvar2 = "John"
ILLEGAL VARIABLE NAMES
➢ 2myvar = "John"
➢my-var = "John"
➢my var = "John"
ASSIGN MULTIPLE VALUES
➢ Python allows you to assign values to multiple variables in one line:
➢Example
➢x, y, z = "Orange", "Banana", "Cherry"
➢print(x)
➢print(y)
➢print(z)
ONE VALUE TO MULTIPLE VARIABLES
➢ And you can assign the same value to multiple variables in one line:
➢Example
➢x = y = z = "Orange"
➢print(x)
➢print(y)
➢print(z)