This document provides an introduction to the Python programming language. It discusses Python language elements like literals, keywords, identifiers, operators, expressions, statements, and comments. It also covers Python data types, variables, printing values, and how to write and run a simple Python program. The document aims to explain Python concepts in a step-by-step manner to help readers learn the basics of the Python language.
2. Topics
What is programming language?
Simple program in Python
Python – Data types
Python – Comments
Python
3. We use natural languages to communicate among
us.
Mathematicians express in terms of equations.
Chemists use formulas to communicate.
How about software developers?
They use programming languages(Python, Java,
.NET, C, C++, etc ) to communicate with
computers.
Python
4. How to master a programming language?
Three steps
1. Learn elements in a language as per language
specification - language tokens ( A-Z,0-9 etc)
2. Learn how to form statements rightly as per
language specification – Syntax ( ‘He is tall’ – right,
‘He are tall’ – wrong syntax)
3. Learn to write right statements to carry out
given task as per user/customer requirements-
semantics – I can and can I? use same words but
provides different meaning.
Python
5. Python Language elements
Token Description Examples
Literals/
values/
constant
Value that remains same everywhere in the
program.
Values belong to different data types
Number. Numeric value 10
doesn’t change. Hello is a text
value which consists of
characters.
keywords Words used by Python to structure the program
and carries specific meaning.
if, continue
Identifiers
(variables/
functions)
Variable name given to store value that might
change
Function name denotes set of statements in the
program
Temperature=35
Temperature=40
Temperature value changes
Operators Symbols to perform operations ( arithmetic,
logical, relational etc)
+, -, *, / , and, or, =>, <
Expressions Combination of values, operators and variable
to produce a value
X=4+5
Statements Instruction given to computer to perform an
action
For, if
comments Add hints to program and should be ignored
during execution
# hello world program
6. Let us write a shortest Python program to print the
word hello in screen.
Create a textfile in notepad or other text editor and
type the following line
print(“hello”)
then save the file in the following format.
Filename.py (eg. hello.py)
Then run the python program as below
python hello.py
It will print the text hello in the screen.
Is not simple to create and run a python program?
Python Language elements
7. Type python and file name in the command line
python hello.py
How to run Python program
8. # is used to mark comment.
Text that follows # will not be executed.
Used to write hints for future use.
Python comments
9. Let us put the text inside two double quotes next to
print inside brackets
print(“hello”)
In case of numbers, the same print will work
print(10). Note that there is no double quotes in
case of numbers.
Python – Printing characters(text) and numbers
10. print(10) is different from print(“ten”)
10 is a number. “ten” is set of
characters(text/string/chars).
We can perform different operations on
number.(Eg. 10 + 20, 10 -5, 10*5 etc)
Whereas chars support different set of operations
(Eg. Length of a given word, concatenating two
words, occurrence particular letter in a word etc)
Python supports specific set of operations on
different type of values.
Python – values
11. Let us try Python in shell
Type Python in command line
type is a function that returns data type for given value.
type(10) returns int and type(“10”) returns str.
Print is a function. Let us see functions usage later.
Python – Data types
12. Values, values attributes and operations form data
types.
Eg integer can represent numbers using digits.
Can do +,-,* etc.
Python – Data types
Numbers
• Integer
• Float
• Boolean
• Complex
Number
Mappings
• Dictionary
Sequence
• String
• List
• Tuple
• Bytes
Set types
• Sets
Python Data Types
13. Keywords must not be used as identifier/variables
name.
Source:https://docs.python.org
Python – Keywords
14. Want to store some value during program execution in computer
memory and refer that value using a name as we wish – identifier
Variable is one type of identifier.
There is no need to mention datatype name while declaring variables in
python. In other languages we need to declare variables with data type.
marks= 80 // marks is an integer variable
Name = “vijay” // Name is a string variable
Variables promotes readability
print(marks) is better than print(80)
Variables promotes reusability
Python – Identifier
15. Want to store some value during program execution in computer
memory and refer that value using a name as we wish – identifier
Variable is one type of identifier.
Values can be stored in variables for future use.
marks= 80 // marks is an integer variable
Name = “vijay” // Name is a string variable
print(marks)
print(Name)
Variables promotes readability in the program.
print(marks) is better than print(80)
Variables promote reusability
Total=mark1 + mark2
Value of mark1 and mark2 are used to assign new value for another
variable Total
Python – Identifier
16. Variables promote maintainability in the code.
print(3.14)
Print(3.14)
Print(3.14)
If you want to change 3.14 into 3.16 you need to change three lines
print(3.16)
print(3.16)
print(3.16)
Let us store 3.14 in a variable, then print three times
pivalue = 3.14
print(pivalue)
print(pivalue)
Print(pivalue)
Let us change pivalue= 3.16. This only change is enough to print 3.16
three times.
Python – Identifier
17. Rules for variablenames
There is no need to mention datatype name while declaring variables in
python. In other languages we need to declare variables with data type.
• Must start with a letteror the underscorecharacter
• Variable names cannot start with a number
• A variable name can only contain alpha-numericcharacters and underscores(A-z,
0-9, and _ )
• Variable names are case-sensitive (name, Name and NAME are three different
variables)
Valid variable names
num, total, total6
Invalid variable names
6total - starts with number
Total* - contains special char
global – it’s a Python key word
Python – Identifier
19. Python Language elements
Token Description Examples
Literals/
values/
constant
Value that remains same everywhere in the
program.
Values belong to different data types
Number. Numeric value 10
doesn’t change. Hello is a text
value which consists of
characters.
keywords Words used by Python to structure the program
and carries specific meaning.
if, continue
Identifiers
(variables/
functions)
Variable name given to store value that might
change
Function name denotes set of statements in the
program
Temperature=35
Temperature=40
Temperature value changes
Operators Symbols to perform operations ( arithmetic,
logical, relational etc)
+, -, *, / , and, or, =>, <
Expressions Combination of values, operators and variable
to produce a value
X=4+5
Statements Instruction given to computer to perform an
action
For, if
comments Add hints to program and should be ignored
during execution
# hello world program