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

002 - 08-05-2020 XI-Python Basics - Programming #1

The document provides an introduction to Python programming, focusing on data types such as literals, integers, floats, strings, and booleans. It explains the use of arithmetic operators, the precedence of operations, and the concept of variables, including naming conventions and how to create and use them. Additionally, it includes quizzes to test understanding of the material covered.

Uploaded by

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

002 - 08-05-2020 XI-Python Basics - Programming #1

The document provides an introduction to Python programming, focusing on data types such as literals, integers, floats, strings, and booleans. It explains the use of arithmetic operators, the precedence of operations, and the concept of variables, including naming conventions and how to create and use them. Additionally, it includes quizzes to test understanding of the material covered.

Uploaded by

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

Python Basics

Computer Science
Programming with Python - 1
Literals - the data in itself 2

A literal is a data, whose value is determined by the literal itself.


Let us take example of a some of the values as follows:
● 123
● 1252.75
● "Welcome to 11"
● True
Each of the above values represent a data. However, owing to their
different data types, they are categorised as following:
● int Integer
● float Floating point
● str String
● bool Boolean
Data types - Integer, Float, String or 3
Boolean
The type of each of the different data as given in the previous slide can be
determined by the command type() in Python as follows:
Integer type
>>> type(123) data
<class 'int'>
>>> type(1252.75) Float type data
<class 'float'>
>>> type("Welcome to 11") String type data
<class 'str'>
Bool or Boolean type
>>> type(True) data
<class 'bool'>
Integers vs. Floats 4

The decimal point is essentially important in recognizing floating-point


numbers in Python.

4 is an int

4.0 is a float

You may think that they are exactly the same, but Python sees them in a
completely different way.

It is also important to note that it is not only a decimal point which makes
a numerical value float. A float value can also be represented using the
exponent notation E. For example 4E8 is a float value representing 4 x
108
Strings 5

● Strings are used when you need to process text (like names of all
kinds, addresses, novels etc.), not numbers.
● Strings need quotes the way float need points.
● You may enclose a string either within a set of single quotes or double
quotes.

The following are some valid examples of strings in Python:

"This is a string"
'This is a string'
"It's Correct"
'I am "Monty Python"'
"I am 'Monty Python'"
Boolean values 6

Boolean values are determined by the truthfulness of a logical statement.


For example, each time when you ask whether one number is greater than
another, it would result into an answer which is either True or False
denoted as 1 and 0.

>>> type(True) Converts the boolean literal True into its


<class 'bool'> equivalent integer 1
>>> int(True)
1
>>> type(False) Converts the boolean literal False into its
<class 'bool'> equivalent integer 0
>>> int(False)
0
Quiz 7

What type of literal is the following example?

"207"

a. int
b. float
c. str
d. bool

Ans:
c. str
Quiz 8

What types of literals are the following four examples?

a. "Monty Python's circus"


b. 2.0
c. 528
d. False

a. str
b. float
c. int
d. bool
Arithmetic operators 9

Python uses the following arithmetic operators upon different type of data
depending upon compatibility of the operator with the literal upon which it
is used:
Pno Name Symbol Example
Result

1. Exponential ** 3**2
9
6.25 ** 0.5
2.5
2. Multiplication * 4 * 3
12
2.5 * 2
5.0
Arithmetic operators 10
Pno Name Symbol Example
Result
3. Division / 3/2
1.5
6.0/3
2.0
4. Floor Division // 7 // 2
3
5 //
2.0 2.0
5. Remainder/Modulo % 7 % 2
1
7.0 % 5
2.0
Precedence of Operators 11

Se Operators Se Operators
q q
1 () 5 + - (Binary Operators)
2 ** 6 <= < > >=
3 + - (Unary Operators) 7 == !=
4 * / % // 8 = %= /= //= -= += *= **=
Evaluation of arithmetical expression 12

Result of an expression formed by combination of multiple arithmetic


operators and data is always evaluated according to the order of
precedence of the operators in it. However the precedence may be altered
by writing an operation within parenthesis. For example:
Expression Result
2 1

2 + 3 * 5 17
1 2

(2 + 3) * 5 25
3 1 4 2

2 - 3 * 5 + 4 / 2 -11.0 Note: 4/2 = 2.0


4 3
2 1 9/3 = 3.0
2 + 3 * (5 - 4) / 2 3.5
Interesting Facts of Arithmetic 13
Operations
While all arithmetic operators are left bound, the exponential operator **
is always right bound i.e. consecutive occurrences of ** operator in an
expression is always evaluated starting with the 2 operands in the right.
2 1

>>> 2 ** 3 ** 2 >>>2**(3**2)
512
512
>>>(2**3)**2
64

1 2

>>> 16 % 5 % 3 >>>(16 % 5) % 3
1 1
>>>16 % (5 % 3)
0
Interesting Facts of Arithmetic 14
Operations
● A division operator / always gives the result of division in float i.e.
with a decimal point irrespective of whether the operands are integer
or float.
● A floor division // operator gives the nearest smaller integer of the
division result. (If Numerator or Denominator is float, the result is float equivalent
to the int result)
● A modulo operator % evaluates the result of remainder of the division
operation upon the two operands for example Numerator %
Denominator, is always calculated using the convention:
Numerator = Quotient * Denominator + Remainder
or, Remainder = Numerator - Quotient * Denominator
where Quotient is the result of the Numerator // Denominator
(Note the floor division)
Interesting Facts of Arithmetic 15
Operations
>>> 7 / 2 >>> -7 // 2
3.5 -4
>>> 4 / 2 >>> -7 % 2
2.0 1
>>> 7 // 2 >>> 7 % -2
3 -1
>>> 7.0 //2 >>> 7.5 % -2
3.0 -0.5
>>> -7 / 2 >>> - 7.5 % 2
-3.5 0.5
>>> 7 % 2
1
Interesting Facts of Arithmetic 16
Operations
Strings can be operated with the arithmetic operators * and
+
With multiply operator *, if one With the addition operator +, if one
of the operands is a string then of the operands is a string the the
the other operand must be an other operand must also be a
integer. In such a case the string string. In such a case the first
is replicated the integer number string operand is concatenated
of times. Example: with the second string operand.
>>> "Hello" * 5 Example:
'HelloHelloHelloHelloHello' >>> "Hello" + "Class 11"
>>> "*" * 20 'HelloClass 11'
'********************' >>> "Welcome "+"To "+"Python"
'Welcome To Python'
Variables - What are variables? 17

We have seen that arithmetic operations upon data gives a result.


However, if we need to use these results of operations in other
instructions, then we shall need to store these results somewhere.

Python provides containers to store such values. These containers are


called variables and as the name suggests, the value stored in such a
container can be varied or altered at any point of execution.

Python variables have: VAL


230 ALUE
V
"JA VALUE UE
00. CK 24
● A variable name. 50 T."
● An associated value.
VARIABLE VARIABLE VARIABLE
AMOUNT NAME ROLLNO
Variables - Naming conventions 18

Variable names must follow some strict rules:

● The name of the variable can be composed of uppercase or lowercase


letters, digits, and the character _ (underscore)
● The name of the variable must begin with a letter or an underscore _;
● Upper and lower case letters are treated as different (for example:
Marks and marks are two different variables);
● The name of the variable must not be any of Python's reserved words
(the keywords - we will learn about this soon).
Python keywords 19

Words with special meaning/purpose in a language are known as


keywords
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Variables - Naming conventions 20

Following are some examples of Following are some examples of


correct variable names: incorrect variable names:

My_name My name Contains space


days_of_year days#of#year Contains #
_Class_Section class Python Keyword
Chapter1 1stChapter Starts with digit
Name
(Can you guess why they are
FOR
wrong?)
Creating Variables 21

A variable comes into existence as a result of assigning a value to it.


If you assign any value to a nonexistent variable, the variable will be
automatically created.
The creation or its syntax is simple: just use the name of the desired
variable, then the equal sign (=) and the value you want to put into the
variable.
Example:
My_name = "Monty Python"
The above instruction first creates a variable named My_name, and then
assigns a string literal "Monty Python" to the created variable My_name.
Using Variables 22

You're allowed to use as many variable declarations as you need to


achieve your goal. Example:
>>>value = 1
>>>account_balance = 1000.0
>>>client_name = 'John Doe'

The type of the value assigned to a created variable decides the type of
the variable:
>>> type(value)
<class 'int'>
>>> type(client_name)
<class 'str'>
Using Variables 23

However, you are not allowed to use a variable which has not been
created i.e. which does not have a value assigned to it. For example the
following statement will throw an error:
>>> Age = 25
>>> type(age)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
type(age)
NameError: name 'age' is not defined

The reason of the error above is that we tried to find type of variable age,
though the variable which we had created was Age.
Using Variables 24

A variable in Python mutates itself according to the data assigned to it at


any point of execution. For example:

>>> Value="Welcome"
>>> type(Value)
<class 'str'>
>>> Value = 25
>>> type(Value)
<class 'int'>
>>> Value = 4/2
>>> type(Value)
Can you guess why the type
<class 'float'> of Value is float here ?
Using Variables 25

The value stored in a variable can be altered at any point of execution,


using either a literal or by the value of another variable or an
operation upon variables/literals. For example:
>>> X = 25 Variable Y is assigned value
>>> Y = X of X
>>> X
25
>>> Y
25
Variable X is assigned Sum of
>>> X = Y + 15 value of variable Y and 15
>>> X
40
Using Variables 26

Variable Y is assigned twice


>>> Y = Y * 2 its previous value
>>> Y
50
>>> X = X + Y
Variable X is assigned Sum
>>> X
of its previous value and
90
value of variable Y
>>> X = "Hello"
>>> Y = "Class"
>>> Z = X + " " + Y + " Eleven"
>>> Z
'Hello Class Eleven'
Quiz 27

Find the outputs of the following code for the red texted
commands:
>>>ver = "3.8" 'Python version3.8'
>>>"Python version" + ver
>>>X=3
>>>Y=2 6561
>>>X**Y**X
>>>Z=X+Y '3.83.83.83.83.8'
>>>ver*Z <class 'str'>
>>>type(ver*Z)
>>>Z = X // Y 1
<class 'int'>
>>>Z
>>>type(Z)
28

Thank You
Computer Science Department
Delhi Public School, R.K.Puram

You might also like