0% found this document useful (0 votes)
4 views

IX_PYTHON_BASICS

The document provides an overview of Python basics, including statements, comments, tokens, variables, identifiers, keywords, literals, and data types. It explains how to use the print() and input() functions, as well as the concept of operators and their categories. Additionally, it covers various data types in Python such as numbers, sequences, sets, and mappings, along with examples for better understanding.

Uploaded by

knownbody0
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)
4 views

IX_PYTHON_BASICS

The document provides an overview of Python basics, including statements, comments, tokens, variables, identifiers, keywords, literals, and data types. It explains how to use the print() and input() functions, as well as the concept of operators and their categories. Additionally, it covers various data types in Python such as numbers, sequences, sets, and mappings, along with examples for better understanding.

Uploaded by

knownbody0
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/ 8

PYTHON BASICS

Python Statement
● Instructions written in the source code for execution are called statements.
● There are different types of statements in the Python programming language like
Assignment statement, Conditional statement, Looping statements etc.
● For example, n = 50 is an assignment statement.

Multi-line statement
● In Python, end of a statement is marked by a newline character.
● However, Statements in Python can be extended to one or more lines using
parentheses (), braces {}, square brackets [], semi-colon (;), continuation character
slash (\).
Examples:

Comments
A comment is text that are non-executable statements that don't affect the outcome
of a code. In Python, we use the hash (#) symbol to start writing a comment.
Python Output Using print() function
We use the print() function to output data to the standard output device (screen). We can
also output data to a file.
An example is given below.
>>> a = "Hello World!"
>>> print(a)
The output of the above code will be : Hello World!

input() Function
In all the examples till now, we have been using the calculations on known values
(constants).
In python, input() function is used to accept a value from user.

Tokens

Any small individual unit of a program is a Token.


For ex: Variables, Identifiers, Keywords, Literals, Operators and Punctuators.

Variable
A variable is a named location used to store data in the
memory.
It is helpful to think of variables as a container that holds
data which can be changed later throughout programming.
For example,
x = 42
y = 42

These declarations make sure that the program reserves


memory for two variables with the names x and y.
The variable names stand for the memory location.

Note:
Assignment operator is used in Python to assign values to variables.
For example, a = 5 is a simple assignment operator that assigns the value 5 on the right
to the variable a on the left.
Identifiers
● Identifier is a name given to user defined variables, functions, class etc.
● It helps to differentiate between variables or other entities.

Rules for defining an Identifier :

Note :
● Python is a case-sensitive language. This means, Variable and variable are not
the same. Always name identifiers that make sense.
● While, c = 10 is valid. Writing count = 10 would make more sense and it would be
easier to figure out what it actually stores.
● Multiple words can be separated using an underscore,
for example : this_is_a_long_variable

Keywords
Keywords are the reserved words in Python which have special meaning used by a
Python interpreter to recognize the structure of the program.
The list of all the keywords is given below.
Literals (Constants) :
A Literal or constant is a type of variable whose value cannot be changed.
It is helpful to think of constants as containers that hold information which cannot be
changed later.
● Integer Literals : 0, 25,120,12500 etc
● Float Literals : 3.15, 6.025 etc
● String Literals : ‘DPS’, “Python Programming” etc
● Boolean Literals : True, False

Assigning Value to a constant in Python


In Python, constants are usually declared and assigned on a module.
Here, the module means a new file containing variables, functions etc. which is imported
to the main file.
Inside the module, constants are written in all capital letters and underscores separating
the words.
Example : Declaring and assigning value to a constant

Rules and Naming convention for variables and constants

Multivariable assignment :
We can assign multiples values to multiple variables
>>> a,b,c = 10,20,30
>>> print(a,b,c)
Output : 10 20 30

We can also assign same values to multiple variables


>>> a=b=c=10
>>> print(a,b,c)
Output : 10 10 10
Python Operators
● Operators are special symbols which represent computation. They are applied on
operand(s), which can be values or variables.
● Same operators can behave differently on different data types. Operators when
applied on operands form an expression.
● Operators are categorized as Arithmetic, Relational, Logical and Assignment.
● Value and variables when used with operator are known as operands.

I Arithmetic Operators

II Relational operators (Comparison Operators)


Comparison operators are used to compare values. It either returns True or False
according to the condition.
III Logical operators
Logical operators are the and, or, not operators.

IV Assignment operators
Assignment operators are used in Python to assign values to variables.
Data Types
Every value in Python has a datatype. Since everything is an object in Python
programming, data types are actually classes and variables are instance (object) of these
classes. There are various data types in Python.
Some of the important types are mentioned below in the image

1) Numbers : It stores Numerical Values.


These are of three different types :
a) Integer
Range of an integer in Python can be from -2147483648 to 2147483647, and
long integer has unlimited range subject to available memory.
Integers are the whole numbers consisting of + or – sign with decimal digits like 100000,
-99, 0, 17.
Also, integers should not have leading zeros.

b) Floating Point: Numbers with fractions or decimal point are called floating point
numbers.
A floating-point number will consist of sign (+,-) sequence of decimals digits and a dot
such as 0.0, -21.9, 0.98333328, 15.2963.
These numbers can also be used to represent a number in engineering/ scientific
notation. -2.0 x 105 will be represented as -2.0e5 = 2.0X10-5 will be 2.0E-5

c) Complex Numbers : It is the combination of both real and imaginary numbers.


Example : a + ib

2) None
This is a special data type with a single value.
It is represented by None (Blank or empty space).
3) Sequence
● A sequence is an ordered collection of items, indexed by positive integers.
● It is a combination of mutable(can be changed) and non-mutable data types.
● Three types of sequence data type available in Python are :
a) Strings b) Lists c) Tuples

String
● String is an ordered sequence of letters/characters.
● They are enclosed in single quotes (‘ ‘) or double (“ “).
● The quotes are not part of string. They only tell the computer where the string
constant begins and ends.
● They can have any character or sign, including space in them.

Lists
● List is also a sequence of values of any type.
● Values in the list are called elements / items. These are indexed/ordered.
● List is enclosed in square brackets.
Example : dob = [19,"January",1990]

Tuples:
● Tuples are a sequence of values of any type, and are indexed by integers.
● They are immutable. Tuples are enclosed in ().
Example : T = (5,'program',2.5)

4) Sets
Set is an unordered collection of values, of any type, with no duplicate entry.
Example: >>> a = {1,2,2,3,3,3}
>>> a {1,2,3}

5) Mapping This data type is unordered. Dictionaries fall under Mappings.


Dictionaries
● Dictionary is an unordered collection of key-value pairs. It is generally used when
we have a huge amount of data.
● Dictionaries are optimized for retrieving data. We must know the key to retrieve the
value.
● In Python, dictionaries are defined within braces {} with each item being a pair in
the form key: value. Key and value can be of any type.
Example : >>> d = {1:'Ajay','key':2}
>>> type(d)

Prepared by :
Hitesh Pujari

You might also like