0% found this document useful (0 votes)
1 views14 pages

Ch1 Python Fundamental

The document provides an overview of Python fundamentals, including data types, variables, and their manipulation. It covers tokens, variable naming conventions, assignment, output and input methods, and the five standard data types: Numbers, Strings, Lists, Tuples, and Dictionaries. Additionally, it discusses data type conversion and the use of comments in Python programming.

Uploaded by

sheik8610
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)
1 views14 pages

Ch1 Python Fundamental

The document provides an overview of Python fundamentals, including data types, variables, and their manipulation. It covers tokens, variable naming conventions, assignment, output and input methods, and the five standard data types: Numbers, Strings, Lists, Tuples, and Dictionaries. Additionally, it discusses data type conversion and the use of comments in Python programming.

Uploaded by

sheik8610
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
You are on page 1/ 14

I PYTHON FUNDAMENTALS

DATA TYPES AND VARIABLES

LEARNING OBJECTIVES
Tokens:
keywords, identifiers, literals, operators and delimiter

variable and methods to manipulate it


Assigning values to variables
output a variable
input a variable
Data Types:
Numbers, String, List, Tuple, Dictionary
Data type conversion:
Implicit and Explicit
Comments

HISTORY OF PYTHON
Python was developed by Guido van Rossum in 1989 at the National Research Institute
for Mathematics and Computer Science in Netherlands.

Python got its name from a BBC comedy series from seventies- “Monty Python’s Flying
Circus”.

TOKENS

Tokens are the smallest individual unit of a program.

Following are the ,tokens in Python:

keywords ,identifiers, literals, operators, and delimiters .

KEYWORDS
● They are the words used by Python interpreter .
● As these words have specific meaning for interpreter,.
● they cannot be used for any other purpose.

To get a list of Keywords type the following commands at the prompt:

>>>import keyword >>>keyword.kwlist

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else',
'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

NOTE :
All these keywords are in small alphabets, except for False, None, True, which are
starting with capital alphabets.

IDENTIFIERS:
These are the names given to identify any memory block, program unit

Examples of identifiers are:


num, roll_no, name etc.

LITERALS :
A fixed numeric or non-numeric value is called a literal.
Examples of literals are:

502, -178.76 "Raja" etc.

OPERATORS :
They perform some action on data. Examples of operators are:
+, -, **, / etc.

DELIMITERS:
Delimiters are the symbols which can be used as separators of values, or to enclose
some values. Examples of delimiters are:
(){}[],;:
NOTE :
# symbol used to insert comments is not a token.

WHAT IS A VARIABLE?

Variables are like containers which are used to store the values for some input,
intermediate result of calculations or the final result.

VARIABLE NAMING CONVENTIONS

Variable names are case sensitive. For eg. num and NUM are treated as two different
variable names.

Keywords should not be used as the variable names.


Variable names should be short and meaningful.
All variable names must begin with a letter or an underscore (_).
After the first letter, variable names may contain letters and digits (0 to 9) and an
underscore (_),
no spaces or special characters are allowed.
Examples of valid variable names: sum, marks1, first_name, _money

Examples of invalid variables names:


marks%, 12grade, class, last-name

ASSIGNING VALUES TO VARIABLES


● We do not need to explicitly declare the variable.
● Variables are declared automatically when they are assigned value.
● The assignment operator (=) is used to assign value to a variable.

EXAMPLES
a=17
name='Rachit'

num=10.5

NOTE : Variables do not need to be declared with any particular type and can even
change type after they have been set.

EXAMPLE

num=10

num = 'Raman'

assign a single value to several variables simultaneously.


For example:

>x=y=z=10

assign multiple values to multiple variables in a single statement.

For example:

>>> x,y,z=12, 70.5, "Vikul"

L-value and R-value

L-value is a value which has an address.


L-value often represents as identifier.

R-value refers to data value

EXAMPLE
>>>a=1

# here the variable a refers to as l-value and 1 is the R-value

>>> b = a

# valid, as l-value can appear on right

>>> 9=a

# error, as 9 is not a l-value

>>>a + 1 = b

# Error, left expression is not variable

>>>a = p + 5

# valid, as "p + 5" is an r-value

OUTPUT A VARIABLE
The print statement is used to display the value of a variable.

PRINT STATEMENT

● The print statement is used to display the value of a variable.

● If an expression is given with the print statement, it first evalutes the expression
and then print it.

● To print more than one item on a single line, comma (,) may be used.

● By default, print uses a single space as a separator and a \n as a terminator


(appears at the end of the string).

● Both of these defaults can be changed to desired values.


>>>x="welcome"

>>>print (x)

welcome

>>> x="Year"

>>> y="New"

>>> print(y + x)

New Year

>>> print('a', 'b', 'c', 'd')

abcd

>>> print('a', 'b', 'c', 'd', sep='@', end='!!')

a@b@c@d!!

NOTE :
print can also be called with no arguments to print a blank line.
a,b=5,7
print(a)
print()
print(b)

output:

INPUT A VARIABLE
input statement
input() returns this value which is usually stored in a variable.

For example:

>>>name = input("What is your name?")

NOTE :
● To input numeric data from input() function we can use typecasting, i.e.,
changing the datatype using function.

● The string data accepted from the user can be converted to appropriate numeric
data type using int(), float() and eval() functions.

>>>y=int (input("enter your marks"))


m=y+5
print(m)

enter your marks 98

Data entered by the user is converted from string to integer and then assigned to y.

DATA TYPES

Data type states the way the values of that type are stored, the operations that can be
done on that type, and the range for that type.

Different types of data like character, integer and numbers with decimals etc. can be
stored in variables.

Different kinds of data require different amount of memory for storage.

Python offers five standard data types:


1. Numbers
2. String
3. List
4. Tuple
5. Dictionary

1. Number

Number data type is used to store Numerical Values.


Python supports three different numerical types

1. integer
2. float point numbers
3. complex numbers

In addition, Booleans are a subtype of integers.

a) Integer are whole numbers without decimal point. They can be positive or negative
with unlimited length.
For example:

>>>a=20

Integers contain Boolean Type which is a unique data type, consisting of two constants,
True & False. A Boolean True value is Non-Zero, Non-Null and Non-empty. For example:

>>>entry=true

b) Numbers with fractions or decimal point are called floating point numbers. For
example:

>>> k=200.789

c) A complex number is an ordered pair of two floating-point numbers denoted by a + bj,


where a and b are the real numbers and j is the imaginary unit. For example:

>>>x=10+5j

For accessing different parts of variable a; we will use a.real and a.imag.

>>>print (x.real, x.imag)

10.0 5.0

2. STRINGS

String is an ordered set of characters enclosed in single or double quotation marks.

For example:

>>>str="I love Python"

3. LISTS

A list is a collection of comma-separated values (items) within square brackets.

Values in the list can be modified, i.e. it is mutable.

The values that make up a list are called its elements.


Elements in a list need not be of the same type.

EXAMPLES

list1=[100, 200, 300, 400, 500]

list2=["Raman", 100, 200, 300, "Ashwin"]

list3=["A", "E", "I", "O", "U"]


4. TUPLE

A tuple is a sequence of comma separated values.

Values in the tuple cannot be modified, i.e. it is immutable.

The values that make up a tuple are called its elements.

Elements in a tuple need not be of the same type.


The comma separated values can be enclosed in parenthesis but parenthesis are not
mandatory.

EXAMPLES

tup1=("Sunday", "Monday", 10, 20)

tup2=("a", "b", "c", "d", "e")

tup3=(1,2,3,4,5,6,7,8,9)

5. DICTIONARY

Python dictionary is an unordered collection of items where each item is a key: value
pair.

We can also refer to a dictionary as a mapping between a set of keys and a set of
values.

Each key is separated from its value by a colon (:),


the items are separated by commas
, and
the entire dictionary is enclosed in curly braces.

. Dictionary is mutable.
We can add new items or change the value of existing items.

EXAMPLES

dict1={'R':'RAINY' , 'S':'SUMMER', 'W':'WINTER' , 'A':'AUTUMN'}

dict2={1: 'robotics', 2:'AR' , 3:'AI' , 4:'VR'}

dict3={'num':10, 1:20}

. We can use the type() function to know the data type of a variable. For example:

>>>num=10

>>> type(num)

<class 'int'>

>>> name='Kapil'

>>> type(name)

<class 'str'>

DATA TYPE CONVERSION

It is a conversion of one data type into another data type.


The conversion can be done explicitly
(programmer specifies the conversions) or
implicitly (Interpreter automatically converts the data type).
For explicit type casting, we use functions:
int ()

float ()

str ()

bool ()

EXAMPLE

>>> x= 158.19

>>> y= int(x)

>>> print (y)

158

EXAMPLE

>
>>>y=float(x)

>>>print y

165.0

COMMENTS

Comments are non-executable statements which are ignored by Python interpreter.

They are used to make the code understandable.

In Python, comment start with "#" symbol and extends till the end of the physical line.

Anything written after # in a line is ignored by interpreter.


A comment can appear on a line by itself or they can also be at the end of line.

Example

# This program calculates the sum of two numbers

>>>n1=10

>>>n2=20
>>>sum= n1 + n2 # sum of numbers 10 and 20

>>>print ("sum=", sum)

For adding multi-line comment in a program, we can:

Place "#" in front of each line


Use triple quoted string.

EXAMPLE

'''This is a

multiline comment'''

or

"""This is a

multiline comment"""

You might also like