Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python Fundamentals

Uploaded by

123jinu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Fundamentals

Uploaded by

123jinu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Python

Fundamentals
• To understand Python fundamentals

Learning
Objectives
In Python, data types are the classification or
categorization of data items.

Data
types
Specifying Data Types

If you want to specify the data type, you can use constructor functions like :
Getting and Setting Data Types

You can get the data type of any object by using the type() function.

For example, to get the data type of a variable x = 5

you would use print(type(x))

When you assign a value to a variable, the data type is set automatically.

For instance,

x = 20 would set x as an int

x = 20.5 would set x as a float


A token is the smallest individual unit in a
program is known as token.

There are five types of token in python:

Tokens • Keywords

• Identifiers

• Literals

• Operators

• Punctuators
Keywords

• A python keyword is a reserved word which you can't use as a


name of your variable, class, function, module and object.
• These keywords have a special meaning and they are used for
special purposes in Python programming language.
• There are 35 keywords in python.
• All the keywords are in lowercase except three keywords (True,
False, None).
Identifiers

Python identifiers are user-defined names.

They are used to specify the names of variables, functions, class, module, and objects.

Rules for Variables:


• It can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an
underscore.
• It cannot start with a digit.
• Keywords cannot be used as an identifier.
• We cannot use special symbols like !, @, #, $, %, + etc. in identifier.
• _ (underscore) can be used in identifier.
• Commas or blank spaces are not allowed within an identifier.
Difference between Keywords and
Variables
IDLE Colour Coding

When you type a program in IDLE some pieces of code are shown in different colours:
• A predefined function is shown in purple.
• A string is shown in green.
• Comments are shown in red.
• If you make a mistake you may spot the colours are wrong.

Keywords
IDLE Colour Coding
Literals
Literals can be defined as a data or value that is assigned to
a variable or a constant.
Numeric
There are three types of numeric literal:

• Integer -Both positive and negative numbers including 0.


e.g. 4, -5, 12
• Float - These are real numbers having both integer and fractional parts.
e.g. 5.4, -3.8
• Complex - The numerals will be in the form of a + bj, where ‘a’ is the real part and ‘b‘ is the complex part.
e.g. 7+5j
Character
It is a set of valid characters that a language recognizes.
It is also a type of Python string literal where a single character is surrounded by single or double quotes.

Special
Letters Digits
Symbols Whitespace
A-Z, a-z 0-9
@!$
Strings
A string is literal and can be created by writing a text (a group of
characters ) surrounded by a single(”), double(“), or triple quotes.
We can write multi-line strings or display them in the desired way
by using triple quotes.
Boolean
There are only two Boolean literals in Python. They are true and false.
In Python, True represents the value as 1, and False represents the value as 0.
Special
Python contains one special literal (None).
‘None’ is used to define a null variable.
If ‘None’ is compared with anything else other than a ‘None’, it will return false.
Literal Collections
Python provides four different types of literal collections:

• List
• Tuple
• Dictionary
• Set
List
• The list contains items of different data types.
• The values stored in the List are separated by a comma (,) and enclosed within square brackets([]).
• We can store different types of data in a List. Hence list are heterogenous.
• Lists are mutable.
Tuple
• Tuple is a collection of different data-type.
• It is enclosed by the parentheses ‘()‘ and each element is separated by the comma(,).
• Tuples are immutable.
Dictionary

• The dictionary stores the data in the key-value pair. It is enclosed by curly braces ‘{}‘ and each pair is separated by
the commas(,).
• We can store different types of data in a dictionary.
• Dictionaries are mutable.
Set
• Set is the collection of the unordered data set.
• It is enclosed by the {} and each element is separated by the comma(,).
• Sets are mutable.
Operators

In Python programming, Operators in general are used to perform operations on values and variables. These are
standard symbols used for logical and arithmetic operations. In this article, we will look into different types of
Python operators.

OPERATORS: These are the special symbols. Eg- + , * , /, etc.


OPERAND: It is the value on which the operator is applied.

Types of operators in python according to number of operands:


• Unary Operator
• Binary Operator
Unary Operator
• Performs the operation on one operand, meaning it affects only one value or variable.
• Unary operators are commonly used in programming languages to perform various operations such as changing the
sign of a value, incrementing or decrementing a value, or performing logical negation.
• May appear before or after the operand.

Examples of Unary Operators:


Unary minus ( negative number, eg :-5)
Unary plus (positive number, eg: 5)
Increment (++)
Decrement (- -)
Logical NOT (!x)
Binary Operator
• Binary operators are commonly used in programming languages to perform various operations such as
arithmetic operations, logical operations, and bitwise operations.
• Appears between the operands.
Binary
Operators
• Blocks and Indentation
Basic • Statements
terms of a
Python • Expressions

Programs • Comments
Blocks and Indentation

• Python uses indentation to define blocks of code, unlike other languages that use braces.
• This means that the whitespace at the beginning of the line is significant and must be consistent.
• 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.
Statements

A line which has the instructions or expressions.


This could be anything written in Python, such as a command to print something to the console, a variable
assignment, or a control flow statement like if, for, or while.

Python statements can be broadly categorized into simple and compound statements:

Simple Statements: These are the most basic kind of statement that does something. These are single line
statements. Examples include expression statements, assignment statements, and the pass, del, return, import,
continue, and break statements.

Compound Statements: These contain groups of other statements and control the execution flow of the program.
Examples include if, while, for, try, and with statements.
Comments
• Comments are not executed.
• Comments in Python start with the # symbol and are used to explain what the code is doing.
• All characters after the # and up to the end of the physical line are part of the comment and the Python
interpreter ignores them.
• They're crucial for making your code understandable to others and to your future self.
• There are two types of comments in python:
Single line comment
Multi-line comment
Comments Contd..

Single line comment: This type of comments start in a line and when a line ends, it is automatically ends.
Single line comment starts with # symbol.

Example: if a>b: # Relational operator compare two values

Multi-Line comment: Multiline comments can be written in more than one lines. Triple quoted ‘ ’ ’ or “ ” ”) multi-
line comments may be used in python. It is also known as docstring.

Example:
‘’’ This program will calculate the average of 10 values.
First find the sum of 10 values
and divide the sum by number of values
‘’’
Punctuators

• Punctuators in Python are symbols used to structure and organize code.


• They are used to indicate the beginning and end of statements, define data types, and control the flow of
execution.
• Some of the commonly used punctuators in Python are:

Parentheses: () - Used to group expressions and to call functions.


Brackets: [] - Used to define lists and access elements in a list.
Curly Braces: {} - Used to define dictionaries and sets.
Colon: : - Used to indicate the beginning of a block of code, such as in loops, functions, and conditional statements.
Comma: , - Used to separate items in a list, tuple, or dictionary.
Expressions

An expression is a piece of computer code that represents a value.


Expressions in Python are constructs made up of variables, operations, and values that a Python interpreter can
evaluate to produce another value.
They are an integral part of programming in Python, allowing for the execution of operations and the storage of
resulting values.
Examples:
a+b
(a+b) AND (c-d)
Types of
Expressio Types of
Python

ns Expression
s

You might also like