CSD102 Data Science
Session – 16
Introduction to Python Programming
(Python Syntax, Key terminology, Data types)
Ahmedabad University
Topics to be covered
❑ Introduction to Python
❑ Basics of Python Programming
❑ Understanding Python Shell
❑ Comments, Variables
❑ Rules for defining variables
❑ Python Data Types – integers, float, string,
collections
Introduction to Python
Introduction to Python
Python is an easy to learn and very powerful
programming language. It has efficient data structures
and a simple approach to object-oriented programming.
Python’s syntax together with its interpreted nature, make
it an ideal language for scripting and rapid application
development.
The Python interpreter and the python standard library
are freely available for all major platforms from the
Python’s official web site, https://www.python.org/.
Introduction to Python
Why it is called Python?
Python programming language was originally designed by Guido
van Rossum.
When he began implementing Python, he was also reading the
scripts from “Monty Python’s Flying Circus” (a BBC comedy
series). He wanted a short, unique, and slightly mysterious name
of language, so he decided to call the language Python.
Python computer language has nothing to do with reptiles
(pythons).
Introduction to Python Programming
Demonstration using
Python 3 IDLE Shell
Introduction to Python Programming
Demonstration of Python 3 IDLE Shell
Python - Comments
Comments in Python
It is good practice to write comments in program code.
In the header part of your program, you can write your
name, date of program and practical definition.
Comments are also used to describe the program logic.
Python - Comments
Example of Comments in Python Code
# This is the sample comment.
# Program is developed by K Patel.
# Date: 20-August-2020
# Definition: Python program to print Hello Python message.
print ("Hello Python Practicals")
Following # is not a comment as it is written inside a string
>>> print ("#Hello Python Practicals")
#Hello Python Practicals
Variables in Python
What are variables?
Variables are names given to some data or information
that we want to store in computer programs.
For example, our program wants to store the marks of
a student. For that, we can declare the
variable studentMarks using the following python
statement.
studentMarks = 0
Variables in Python
A variable can have a short name (like x and y) or a more descriptive
like (age, studentName, totalVolume).
We can also define multiple variables using single python statement
as shown below:
rollNo, studentMarks, studentName = 101, 90, ‘KP’
Above statement is equivalent to following three statements:
rollNo = 101
studentMarks = 90
studentName = ‘KP’
Variables in Python
Important 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)
Variables in Python
Important Rules for Python variables
Examples of Valid Python Variable Names
studentName, student_name, studentName1, Name2, Age, age1
Examples Invalid Python Variable Names
1_name, 123, while, if, name@1, @123
Use of Undefined Variable
❑ Trying to use undefined variables will result in an error. In
following python code, we are trying to use num variable which
is not defined so far. This will result in error as shown below:
>>> num # trying to use an undefined variable
Traceback (most recent call last):
File “<pyshell#10>”, line 1, in <module>
NameError: name ‘num’ is not defined
Operations on Variables
❑ In interpreter, the last printed expression is assigned to
the variable _. This means that when you are using
Python as a simple calculator, it is easier to continue
calculations, for example:
>>> sum=0
>>> n1=5
>>> n2=10
>>> n1 + n2
15
>>> sum + _
15
This variable should be treated as read-only by the programmer.
Don’t explicitly assign a value to it ( _ variable).
Python Booleans
Booleans represent one of two values: True or False.
Boolean Values
In programming you often need to know if an expression is True
or False.
You can evaluate any expression in Python, and get one of two
answers, True or False.
When you compare two values, the expression is evaluated and
Python returns the Boolean answer:
Python Booleans
Examples
print(10 > 9)
print(10 == 9)
print(10 < 9)
True
False
False
Python Booleans
Examples
When you run a condition in an if statement, Python returns True
or False:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Data Types in Programming
Python Data Types
In programming languages, different data types (e.g. int,
float, string etc.) are used to store different categories of
data (e.g. integer, floating point numbers, string, etc).
Let’s learn some of the basic data types of Python
programming - integer, float and string.
Python also supports many advanced data types like list,
tuple, dictionary, etc. that we will discuss later.
Python Data Types
Integers
Integer data type is used to store numbers with no decimal
parts. Examples of integer data types are 5, 10, 20, 101, -
100, -505 etc.
To declare an integer data type in Python programming,
simply write
NameOfVariable = some initial value
Example:
marks = 85
rollNo = 2019101
Python Data Types
Floating point
More popularly known as float, which refers to numbers that
have decimal parts. Examples are 12.34, 0.001, 0.050,
101.001.
To declare a float variable in Python, write
NameOfVariable = some initial value
Example:
studentHeight = 5.75
studentAge = 21.5
Python Data Types
String
String data type is used to store text. To declare a string variable, you can write
any of the following syntax
NameOfvariable = ‘some initial value’ (single quotes)
or
NameOfvariable = “some initial value” (double quotes)
Example:
StudentName = ‘K Patel’
StudentAddress = “Ahmedabad, Gujarat, India”
studentAge = ’25’
In the last example, studentAge is a string type of variable as it is written in
single quote. In contrast, if we write studentAge = 25 (without quotes), it
becomes is an integer type variable.
Classroom Activity
Python basics, variables and data types:
Which of the following character can be used to specify comments in
Python?
A. #
B. $
C. //
D. none of these
In python, variable name cannot start with a __________.
A. number
B. letter
C. underscore
D. none of these
Classroom Activity
Predict output:
>>> 11 / 2
>>> print(10 == 9)
>>> print(10<8)
Demonstrations
Topics covered and References
Introduction to Python
Basics of Python Programming
Comments
Variables
Rules for defining variables
Python Data Types
https://docs.python.org