0% found this document useful (0 votes)
10 views9 pages

Python Notes 1

The document provides an introduction to Python programming, highlighting its features such as being a high-level, interpreted, and open-source language. It covers the basics of writing and executing Python programs, including the use of an interpreter, character sets, tokens, data types, and operators. Additionally, it explains Python's syntax rules for identifiers, variables, and data structures like lists, tuples, and dictionaries.

Uploaded by

lahok72056
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)
10 views9 pages

Python Notes 1

The document provides an introduction to Python programming, highlighting its features such as being a high-level, interpreted, and open-source language. It covers the basics of writing and executing Python programs, including the use of an interpreter, character sets, tokens, data types, and operators. Additionally, it explains Python's syntax rules for identifiers, variables, and data structures like lists, tuples, and dictionaries.

Uploaded by

lahok72056
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

Getting Started with Python

An ordered set of instructions to be executed by a computer to carry out a


specific task is called a program, and the language used to specify this set of
instructions to the computer is called a programming language.
Eg: Python, C++, Visual Basic, PHP, Java
A program written in a high-level language is called source code.
Language translators like compilers and interpreters are needed to translate
the source code into machine language.
Python uses an interpreter to convert its instructions into machine language
An interpreter processes the program statements one by one, first translating
and then executing. This process is continued until an error is encountered or
the whole program is executed successfully.

Features of Python
• Python is a high-level language. It is a free and open-source language.
• It is an interpreted language, as Python programs are executed by an
interpreter.
• Python programs are easy to understand as they have a clearly defined syntax
and relatively simple structure.
• Python is case-sensitive. For example, NUMBER and number are not same in
Python.
• Python is portable and platform independent, means it can run on various
operating systems and hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many popular web services and
applications are built using Python.
• Python uses indentation for blocks and nested blocks.

Working with Python


Python was created by Guido Van Rossum and was first released in 1991.
To write and run (execute) a Python program, we need to have a Python
interpreter.
Eg: IDLE – Integrated Development and Learning Environment.
Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode
b) Script mode
Interactive mode allows execution of individual statement instantaneously.
Whereas, Script mode allows us to write more than one instruction in a file
called Python source code file that can be executed. Python scripts are saved as
files where file name has extension “.py”

Python Fundamentals
Python Character Set:
It is a group of valid characters that a language can [Link] character
set contains
Letters – A-Z and a-z,Digits – 0-9 , Blankspaces , special characters etc.
Tokens
The smallest individual unit in a program that is meaningful for a
compiler/interpreter is known as token. Python tokens are (KILOP)
K- Keywords
I – identifiers
L- Literals
O-Operators
P- Punctuators

Keywords -Keywords are reserved words. Each keyword has a specific meaning
to the Python interpreter, and we can use a keyword in our program only for
that purpose.

Identifiers:
Identifiers are names used to identify a variable, function, or other entities in a
program.
• The rules for naming an identifier in Python are as follows:
• The name should begin with an uppercase or a lowercase alphabet or an
underscore sign (_). This may be followed by any combination of characters a–z,
A–Z, 0–9 or underscore (_). Thus, an identifier cannot start with a digit.
• It can be of any length. (However, it is preferred to keep it short and
meaningful).
• It should not be a keyword or reserved word
• We cannot use special symbols like !, @, #, $, %, etc.
Follow it up with examples of identifying valid and invalid identifiers.

Variables
We can use an assignment statement to create new variables and assign
specific values to them.
Example:
Write a program to display values of variables in Python.
message = "Keep Smiling"
print(message)
userNo = 101
print('User Number is', userNo)

Variable declaration is implicit in Python, means variables are automatically


declared and defined when they are assigned a value the first time.

Data types
Data type identifies the type of data values a variable can hold and the
operations that can be performed on that data.

Numbers
Number data type stores numerical values only.
It is further classified into three different types: int, float and complex.
Boolean data type (bool) is a subtype of integer. It is a unique data type,
consisting of two constants, True and False.

Sequence
A Python sequence is an ordered collection of items, where each item is indexed
by an integer. The three types of sequence data types available in Python are
Strings, Lists and Tuples.

String
String is a group of characters. These characters may be alphabets, digits or
special characters including spaces.
String values are enclosed either in single quotation.
For example, >>> str1 = 'Hello Friend'
>>> str2 = "452"

List
List is a sequence of items separated by commas and the items are enclosed in
square brackets [ ].

Tuple
Tuple is a sequence of items separated by commas and items are enclosed in
parenthesis ( ).

Dictionary
Dictionary in Python holds data items in key-value pairs. Items in a dictionary
are enclosed in curly brackets { }.The key : value pairs of a dictionary can be
accessed using the key. In order to access any value in the dictionary, we have
to specify its key in square brackets [ ].
>>> dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
>>> print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}

>>> print(dict1['Price(kg)'])
120

Mutable and Immutable Data Types


Variables whose values can be changed after they are created and assigned are
called mutable. Variables whose values cannot be changed after they are
created and assigned are called immutable.
Operators
An operator is used to perform specific mathematical or logical operation on
values. The values that the operators work on are called operands.

Arithmetic Operators

Relational Operators

Relational operator compares the values of the operands on its either side and
determines the relationship among them.
Assume the Python variables num1 = 10, num2 = 0, num3 = 10, str1 = "Good",
str2 = "Afternoon" for the following examples:

Assignment Operators
Assignment operator assigns or changes the value of the variable on its left.
Logical Operators
There are three logical operators supported by Python. These operators are
and, or, not.
Note : By default, all values are True except None, False, 0 (zero), empty
collections "", (), [], {}.
Identity Operators
Identity operators can also be used to determine whether two variables are
referring to the same object or not.
Membership Operators
Membership operators are used to check if a value is a member of the given
sequence or not.

Precedence of Operators

You might also like