IX_PYTHON_BASICS
IX_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
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
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.
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
Multivariable assignment :
We can assign multiples values to multiple variables
>>> a,b,c = 10,20,30
>>> print(a,b,c)
Output : 10 20 30
I Arithmetic 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
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
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}
Prepared by :
Hitesh Pujari