Python: Vks-Learning Hub
Python: Vks-Learning Hub
PYTHON
VKS-LEARNING HUB
What is Python…?
Python is a simple, interpreted, object-oriented, high-level , open source
programming language with dynamic semantics.
Free & Open source
Freely distributed and Open source
Maintained by the Python community
High Level Language
Interpreted
You run the program straight from the source code.
Python program Bytecode a platforms native language
You can just copy over your code to another system and it will work! with
python platform
Object-Oriented
Simple and additionally supports procedural programming
Extensible – easily import other code
Embeddable –easily place your code in non-python programs
Extensive libraries
(i.e. reg. expressions, doc generation, CGI, ftp, web browsers, ZIP, WAV,
cryptography, etc...) (wxPython, Twisted, Python Imaging library)
VKS-LEARNING HUB
Programming basics
• code or source code: The sequence of instructions in a program.
• syntax: The set of legal structures and commands that can be used in a
particular programming language.
• output: The messages printed to the user by a program.
• console: The text box onto which output is printed.
– Some source code editors pop up the console as an external window,
and others contain their own console window.
4
VKS-LEARNING HUB
interpret
5
VKS-LEARNING HUB
Python Interfaces
• IDLE – a cross-platform Python development
environment
• PythonWin – a Windows only interface to
Python
• Python Shell – running 'python' from the
Command Line opens this interactive shell
• For the exercises, we'll use IDLE, but you can
try them all.
VKS-LEARNING HUB
• Hello World
print(“Hello World”)
• prints Hello World
VKS-LEARNING HUB
Since it does not provide a way to save the code you enter, the interactive shell is not
the best tool for writing larger programs. The IDLE interactive shell is useful for
experimenting with small snippets of Python code
IDLE’s editor. IDLE has a built in editor.
From the IDLE menu, select New Window,
Editor will open a file . Type the text print(“VKS-Learning-Hub”) into the
editor.
We can run the program from within the IDLE editor by pressing
the F5 function key or from the editor’s Run menu: Run→Run
Module. The output appears in the IDLE interactive shell window.
If program is not saved it give message to save first before run
You can save your program using the Save option in the File menu as
shown in Figure. Save the code to a file named try1.py. The
extension .py is the extension used for Python source code.
VKS-LEARNING HUB
print(“VKS-Learning Hub")
If you try to enter each line one at a time into the IDLE interactive shell, the program’s
output will be intermingled with the statements you type. In this case the best approach
is to type the program into an editor, save the code you type to a file, and then execute
the program. Most of the time we use an editor to enter and run our Python programs.
The interactive interpreter is most useful for experimenting with small snippets of
Python code.
VKS-LEARNING HUB
It is important that no whitespace (spaces or tabs) come before the beginning of each
statement.
In Python the indentation of statements is significant and must be done properly. If
we try to put a single space before a statement in the interactive shell
Token
Smallest Individual unit in a program is known as a Token or
a Lexical unit.
• Keyword
• Identifiers (Names)
• Literals
• Operators
• Punctuators
VKS-LEARNING HUB
Keywords
• A keyword is a word having special meaning reserved by the
programming language.
Python Identifiers
Literals
1. String Literals 3. Boolean Literals
– Single Line String 4.Special Literals
– Multi Line String 5. Literal Collection
– Raw String – List
– Unicode String – Tuple
2. Numeric Literals – Dictionary
– Integer – Set
– Float
– Complex
VKS-LEARNING HUB
Strings
A String Literal is a sequence of character surrounded by
quotes( Single or double or triple).
Example
strings = "This is Python"
char = "C"
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string“s
print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
Demo1.py
• To calculate length of string use len() function
VKS-LEARNING HUB
Numeric Literals
• Integer
– Decimal Decimal 12, -25, 10035
– Binary Literal Binary 0b1010
– Octal Octal 0o12, 0O35
– Hexadecimal Hexadecimal 0x31A2, 0X4A
• Float Float 3.5, 2000.152,
135.75e25, -3.2e3
• Complex Complex (2 + 3.5j)
Number
VKS-LEARNING HUB
Boolean Literals
• There are two kinds of Boolean literal: True and False.
Example: demo3.py
x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
VKS-LEARNING HUB
Literal Collections
• fruits = [ "apple", "mango", "orange” ] #list
• numbers = (1, 2, 3) #tuple
• alphabets = { 'a':'apple', 'b':'ball', 'c':'cat’ } #dictionary
• vowels = { 'a', 'e', 'i' , 'o', 'u’ } #set
• print(fruits)
• print(numbers)
• print(alphabets)
• print(vowels)
• Demo5.py
VKS-LEARNING HUB
Operators
• Unary Operator (+, -, ~, not)
• Binary Operator
– Arithmetic Operator (+, -, *, /, %, **, //)
– Relational Operator (<, >, <=, >=, ==, !=)
– Logical Operator (and, or, not)
– Bitwise Operator (&, ^, |, <<, >>)
– Assignment Operator (=, +=, -=, *=, /=, %=,**=, //=)
– Identity Operator (is, not is)
– Membership Operator (in, not in)
VKS-LEARNING HUB
Punctuators
• Most common punctuators in python
are-
'"#\()[]{}@,:.=;
VKS-LEARNING HUB
expressions
• An expressions is any legal combination of
symbols(operators) that represent a value
• 15
• 2.9
• A=a+b
• A+4
• D>5
• F=(3+5)/2
VKS-LEARNING HUB
Python Statement
• Instructions that a Python interpreter can execute are called statements.
• For example, a = 2 is an assignment statement
• if statement, for statement, while statement etc. are other kinds of statements.
Multi-line statement
In Python, end of a statement is marked by a newline character. But we can make a
statement extend over multiple lines with the line continuation character (\).
For example:
a=1+2+3+\
4+5+6+\
7+8+9
Multiple Statement in Single Line
We could also put multiple statements in a single line using semicolons, as
follows
a = 1; b = 2; c = 3
VKS-LEARNING HUB
Python Indentation
• Most of the programming languages like C, C++, Java use
braces { } to define a block of code. Python uses
indentation.
• A code block (body of a function, loop, class etc.) starts
with indentation and ends with the first unindented line.
• The amount of indentation is up to you, but it must be
consistent throughout that block.
• Generally four whitespaces are used for indentation and
is preferred over tabs.
VKS-LEARNING HUB
Python Comments
Docstring in Python
• Docstring is short for documentation string.
• It is a string that occurs as the first statement in a module, function,
class, or method definition.
• Triple quotes are used while writing docstrings. For example:
def double(num):
"""Function to double the value"""
return 2*num
• Docstring is available to us as the attribute __doc__ of the function.
Issue the following code in shell once you run the above program.
>>> print(double.__doc__)
Function to double the value : demo6.py
VKS-LEARNING HUB
print
• print : Produces text output on the console.
• Syntax:
print ("Message“)
print (Expression)
– prints the given text message or expression value on the
console, and moves the cursor down to the next line.
print( Item1, Item2, ..., ItemN)
– Prints several messages and/or expressions on the same
line.
34
VKS-LEARNING HUB
print(a, b, c, sep=“:”)
VKS-LEARNING HUB
Variables
• variable: A named piece of memory that can
store a value.
– Usage:
• Compute an expression's result,
• store that result into a variable,
• and use that variable later in the program.
• Variable Definition
print(x) // will give error because x is not define
Dynamic Typing: A variable pointing to a value of a
certain type can be made to point a value/object of
different type. This is called Dynamic Typing.
X=10
X=“hello”
• Caution with Dynamic Typing
Y=10
X=“hello”
Y=X/2 /// ERROR
VKS-LEARNING HUB
input
• input : Reads input data from user.
– You can assign (store) the result of input into a variable. By
default every thing is text /string as input
– Example:
name= input(“Enter your Name“))
age = int(input("How old are you? “))
print ("Your Name is”,name,”and age is",
age)
print("You have", 65 - age, "years left for
retirement“)
Output:
Enter your Name Vinod
How old are you? 53
Your Name is Vinod and age is 53
You have 12 years left for retirement
41
VKS-LEARNING HUB
VKS-LEARNING HUB