0% found this document useful (0 votes)
0 views21 pages

DS Lab 01

The document provides an overview of Python's basic syntax, including identifiers, lines and indentation, and data types. It explains how to define variables, assign values, and the characteristics of standard data types such as numbers, strings, lists, tuples, and dictionaries. Additionally, it covers naming conventions, comments, and the use of operators for string and list manipulation.

Uploaded by

kafiamunawar509
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)
0 views21 pages

DS Lab 01

The document provides an overview of Python's basic syntax, including identifiers, lines and indentation, and data types. It explains how to define variables, assign values, and the characteristics of standard data types such as numbers, strings, lists, tuples, and dictionaries. Additionally, it covers naming conventions, comments, and the use of operators for string and list manipulation.

Uploaded by

kafiamunawar509
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/ 21

Python 01

Basic Syntax
• The Python language has many similarities to Perl, C, and Java. However, there are some
definite differences between the languages.
• First code…

• Script Mode Programming


• 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 punctuation characters such as @, $,
and % within identifiers. Python is a case sensitive
programming language. Thus, Manpower and manpower are
Python two different identifiers in Python.

Identifiers
• Here are naming conventions for Python identifiers −
• Class names start with an uppercase letter. All other
identifiers start with a lowercase letter.
• Starting an identifier with a single leading underscore
indicates that the identifier is private.
• Starting an identifier with two leading underscores
indicates a strong private identifier.
• If the identifier also ends with two trailing underscores,
the identifier is a language-defined special name.
Lines and Indentation

• Python does not use


braces({}) to indicate blocks
of code for class and function
definitions or flow control.
Blocks of code are denoted
by line indentation, which is
rigidly enforced.
• The number of spaces in the
indentation is variable, but all
statements within the block
must be indented the same
amount.
Quotation in Python
• Python accepts single ('), double (") and triple (''' or """) quotes to
denote string literals, as long as the same type of quote starts
and ends the string.
• The triple quotes are used to span the string across multiple
lines.
Comments
in Python

• A hash sign (#) that is not inside a string literal is


the beginning of a comment. All characters after
the #, up to the end of the physical line, are part of
the comment and the Python interpreter ignores
them.
• Groups of individual statements, which make a single
code block are called suites in Python. Compound or
complex statements, such as if, while, def, and class
require a header line and a suite.
• Header lines begin the statement (with the keyword)

Multiple
and terminate with a colon ( : ) and are followed by one
or more lines which make up the suite.

Statement
Groups as
Suites
• Variables are nothing but reserved memory locations to store
values. It means that when you create a variable, you reserve
some space in the memory.
• Based on the data type of a variable, the interpreter allocates
memory and decides what can be stored in the reserved
memory.

Variable • Therefore, by assigning different data types to the variables,


you can store integers, decimals or characters in these

Types variables.
Assigning Values to
Variables
• Python variables do not need explicit
declaration to reserve memory space. The
declaration happens automatically when
you assign a value to a variable. The equal
sign (=) is used to assign values to
variables.
• The operand to the left of the = operator is
the name of the variable and the operand
to the right of the = operator is the value
stored in the variable.
Multiple Assignment
• Python allows you to assign a single value to several variables
simultaneously.
Standard Data Types
• The data stored in memory can be of many types. For example,
a person's age is stored as a numeric value and his or her
address is stored as alphanumeric characters. Python has
various standard data types that are used to define the
operations possible on them and the storage method for each
of them.
• Python has five standard data types:
• Numbers
• String
• List
• Tuple
• Dictionary
Python Numbers
• Number data types store numeric values. Number objects are
created when you assign a value to them.

• You can also delete the reference to a number object by using


the del statement. You can delete a single object or multiple
objects by using the del statement.
• Python supports three different numerical types −
• int (signed integers)
• float (floating point real values)
• complex (complex numbers)

• A complex number consists of an ordered pair of real


floating-point numbers denoted by x + yj, where x
and y are real numbers and j is the imaginary unit.
• Strings in Python are identified as a contiguous set of
characters represented in the quotation marks. Python
allows either pair of single or double quotes. Subsets of
strings can be taken using the slice operator ([ ] and [:] )
with indexes starting at 0 in the beginning of the string
and working their way from -1 to the end.
• The plus (+) sign is the string concatenation operator
and the asterisk (*) is the repetition operator.
Python
Strings
Python Lists
• Lists are the most versatile of Python's compound
data types. A list contains items separated by
commas and enclosed within square brackets ([]).
• To some extent, lists are similar to arrays in C. One of
the differences between them is that all the items
belonging to a list can be of different data type.
• The values stored in a list can be accessed using the
slice operator ([ ] and [:]) with indexes starting at 0 in
the beginning of the list and working their way to end
-1. The plus (+) sign is the list concatenation operator,
and the asterisk (*) is the repetition operator.
A tuple is another sequence data type that is
similar to the list. A tuple consists of a number
of values separated by commas. Unlike lists,
however, tuples are enclosed within
parenthesis.
Python
Tuples The main difference between lists and tuples
are − Lists are enclosed in brackets ( [ ] ) and
their elements and size can be changed, while
tuples are enclosed in parentheses ( ( ) ) and
cannot be updated. Tuples can be thought of
as read-only lists.
Python's dictionaries are kind of hash-table
type. They work like associative arrays or
hashes found in Perl and consist of key-value
pairs.
A dictionary key can be almost any Python
Python type, but are usually numbers or strings.
Dictionary Values, on the other hand, can be any
arbitrary Python object.
Dictionaries are enclosed by curly braces ({
}) and values can be assigned and accessed
using square braces ([]).

You might also like