0% found this document useful (0 votes)
7 views25 pages

Basic Python

The document provides an overview of Python programming, including its history, features, and basic concepts. It covers installation instructions, programming basics, data types, operators, and the structure of a Python program. Key elements such as variables, expressions, and the use of comments are also discussed.

Uploaded by

Prakash Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views25 pages

Basic Python

The document provides an overview of Python programming, including its history, features, and basic concepts. It covers installation instructions, programming basics, data types, operators, and the structure of a Python program. Key elements such as variables, expressions, and the use of comments are also discussed.

Uploaded by

Prakash Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programing

Prof. K. Adisesha
Python!
• Created in 1991 by Guido van Rossum
 Named for Monty Python

• Useful as a scripting language


 script: A small program meant for one-time use
 Targeted towards small to medium sized projects

• Used by:
 Google, Yahoo!, Youtube
 Many Linux distributions
 Games and apps (e.g. Eve Online)

2
Python
• 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 auto-
magically 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)

3
Installing Python
Windows: Mac OS X:
• Download Python from • Python is already installed.
[Link] • Open a terminal and run python
• Install Python. or run Idle from Finder.
• Run Idle from the Start Menu.
Linux:
• Chances are you already have
Python installed. To check, run
python from the terminal.
• If not, install from your
distribution's package system.
Note: For step by step installation
instructions, see the course web site.

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

5
The Python Interpreter
• interpreted
 In Python Code is written and then directly executed by an
interpreter
 Type commands into interpreter and see immediate results
• Allows you to type commands one-at-a-time and see results
• A great way to explore Python's syntax
 Repeat previous command: Alt+P

6
Token

Smallest individual unit in a program is known as token.

• [Link]

• [Link]

• [Link]

• [Link]

• [Link] / Delimiters

7
Keywords
• Reserve word of the compiler/interpreter which
can’t be used as identifier.

8
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).
• Python does not allow special characters
• Identifier must not be a keyword of Python.
• Python is a case sensitive programming language.
• Thus, Rollnumber and rollnumber are two different identifiers in
Python.
• Some valid identifiers :Mybook, file123, z2td, date_2, _no
• Some invalid identifier : 2rno,break,[Link],data-cs

9
Literals
• Literals in Python can be defined as number, text, or
other data that represent values to be stored in
variables.
• Example of String Literals in Python
• name = ‘Johni’ , fname=“johny”
• Example of Integer Literals in Python(numeric literal)
• age = 22
• Example of Float Literals in Python(numeric literal)
• height = 6.2
• Example of Special Literals in Python
• name = None

10
Operators
• Operators can be defined as symbols that are used to
perform operations on operands.

• Types of Operators
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators

11
Arithmetic Operators
• Arithmetic Operators are used to perform arithmetic
operations like addition, multiplication, division etc.

12
Relational Operators
• Relational Operators are used to compare the values.

13
Assignment Operators
• Used to assign values to the variables.

14
Logical Operators
• Logical Operators are used to perform logical
operations on the given two variables or values.

Example:
a=30
b=20
if(a==30andb==20):
print('hello')
Output :-
hello
15
Membership Operators
• The membership operators in Python are used to
validate whether a value is found within a sequence
such as such as strings, lists, or tuples.

• Example:
a = 22
list = [22,99,27,31]
Ans1= a in list Ans2= a not in list
print(Ans1) print(Ans2)
• Output :-
True False

16
Identity Operators
• Identity operators in Python compare the memory
locations of two objects.

• Example:
a = 34
b=34
if (a is b):
print('both a and b has same identity')
else:
print('a and b has different identity')
• Output :-
both a and b has same identity 17
Punctuators / Delimiters
• Used to implement the grammatical and structure of a
Syntax.
• Following are the python punctuators.

18
Python program
• A python program contain the following components

1. Comments
2. Function
3. Expression
4. Statement
5. Block & indentation

19
Python Comment
• Comments: which is readable for programmer but ignored
by python interpreter
a) Single line comment: Which begins with # sign.
• Syntax:
# comment text (one line)
 Example
# This is a comment
b) Multi line comment (doc-string): either write multiple line
beginning with # sign or use triple quoted multiple line. E.g.
 Example
‘’’this is my
first
python multiline comment ‘’’
20
Expressions
• expression: A data value or set of operations to compute a
value.
Examples: 1 + 4 * 3
42
• Arithmetic operators we will use:
 + - * / addition, subtraction/negation, multiplication, division
 % modulus, a.k.a. remainder
 ** exponentiation
• precedence: Order in which operations are computed.
 * / % ** have a higher precedence than + -
1 + 3 * 4 is 13

 Parentheses can be used to force a certain order of evaluation.


(1 + 3) * 4 is 16
21
Type conversion
• The process of converting the value of one data type
(integer, string, float, etc.) to another data type is called
type conversion.
• Python has two types of type conversion.
• Implicit Type Conversion: Python automatically converts one
data type to another data-type. This process doesn't need any user
involvement.
• Explicit Type Conversion: In Explicit Type Conversion, users
convert the data type of an object to required data type. We use the
predefined functions like int(), float(), str() etc.

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

• assignment statement: Stores a value into a variable.


 Syntax:
name = value
 Examples: x = 5 gpa = 3.14

 A variable that has been given a value can be used in


expressions.
x + 4 is 9

23
The print Statement
• Python uses indentation to indicate blocks, instead of {}
 Makes the code simpler and more readable
 In Python, you must indent.
• 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.
• Examples:
print ("Hello, world!“)
age = 45
print ("You have", 65 - age, "years until retirement“)
Output:
Hello, world!
You have 20 years until retirement
24
input
• input : Reads a number from user input.
 You can assign (store) the result of input into a variable.
 Example:
age = input("How old are you? ")
print "Your age is", age
print "You have", 65 - age, "years until
retirement"

Output:
How old are you? 53
Your age is 53
You have 12 years until retirement

25

You might also like