0% found this document useful (0 votes)
2 views11 pages

Python

The document provides an overview of Python programming concepts, including environments, APIs, SDKs, and the differences between procedural and object-oriented programming (POP vs OOP). It covers variables, data types, typecasting, and operators, detailing how to perform input and output operations, as well as the significance of each data type in Python. Additionally, it explains the use of arithmetic, assignment, and comparison operators, highlighting their functionality and examples.

Uploaded by

Parag Mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Python

The document provides an overview of Python programming concepts, including environments, APIs, SDKs, and the differences between procedural and object-oriented programming (POP vs OOP). It covers variables, data types, typecasting, and operators, detailing how to perform input and output operations, as well as the significance of each data type in Python. Additionally, it explains the use of arithmetic, assignment, and comparison operators, highlighting their functionality and examples.

Uploaded by

Parag Mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

An environment is the bundle of various packages, methods which are required for your python code

to run efficiently. Environment influences how your python code will be executed. If there are two
different environments, then the same python code may behave differently. We can manage the
package versions from the jupyter interface itself.

An application programming interface (API) is a set of rules, protocols and tools that enable different
software applications to communicate with each other. APIs act as a bridge between different
systems, allowing one to interact with another without knowing how it is implemented. A software
development kit (SDK) is a complete set of tools, including libraries, APIs and documentation, which
developers can use to create applications for a specific platform. For example, Google Maps offers a
range of APIs and SDKs, which allow developers to integrate Google Maps' functionality into their
applications and websites.

The differences between POP and OOP are as follows:

POP OOP

Focuses on
functions or
sequences of Focuses on objects (entities that combine data and methods) and their
Focus
tasks to interactions
manipulate
data

Linear and
step-by-step
Approach approach to Modular and hierarchical approach to solving problems
solving
problems

Data is
separate
from
Data functions;
Data is encapsulated within objects; methods operate on this data
handling data is
passed
between
functions

Organised as
a sequence
Organised around classes and objects, which bundle data and behaviour
Structure of functions
together
that operate
on data

Code Moderate High reusability through inheritance and polymorphism


reusability reusability
through
functions;
limited by
how data is
manipulated

Less secure
as data is
Data global or Data is protected via encapsulation, restricting access to only certain
security passed methods
between
functions

Not
supported;
each Supported; new classes can inherit the properties and behaviours of other
Inheritance
function acts classes
independent
ly

Moderate
modularity
Modularity High modularity through classes and functions
through
functions

Top-down
approach;
Design focus on
Bottom-up approach; focus on creating reusable objects
philosophy procedures
in a step-by-
step manner

Can be
difficult to
Maintenanc
maintain due Easier to maintain due to the modular design
e
to a lack of
structure

For OOPS concept: [Link]

Ctrl+Enter or Shift+Enter to run the Collab notebook

If you press Text and then pass #Heading, then it will display Heading in big format. To have the sub
heading pass ##.

Using * left and right both will make the statement in italic whereas ** will make it bold

Variables and Datatypes:

A variable acts as a container that stores data. Data can be of various types, from simple
numbers and letters to more complicated structures. When a variable is assigned a value, it is
given a name and a location in memory. Python uses the name to refer to the location in
memory, thereby retrieving its value.

The fundamental data types we will cover in this session are as follows:

● Integers
● Floats
● Strings
● Booleans
● NoneType

We will also cover typecasting, the process of converting data from one type to another. We will also
introduce some basic inbuilt functions in Python for input and output
called input() and print(), respectively.

Input is used to take the data and print to display the data.

In Python, numeric data types are the variables that store data that represent numbers. Usually,
numeric data is of two types:

 Integers, which include positive and negative whole numbers, as well as zero

 Floats (or floating point numbers), which include all real numbers, including numbers with
decimal points

To store these values, you can assign them names:

num1 = 1

num2 = 3.14

You can check the types of these values by using the in-built type() function:

type(num1)

type(num2)

Values can be assigned to a variable in different ways:

a=1

b = c = -2 # assigns -2 to both b and c

x,y,z = 3.0, 4, -5.0 # assigns the values 3.0, 4, and -5.0 to x, y, and z respectively

You can reassign a variable to hold a new value. For example:

number = 0.0

Number = -999

If a number is in base 2, it is called binary. Binary numbers have only two types of digits, 0 and 1. The
same number can be represented in binary as 10011010010.

Strings are a sequence of characters used to represent textual data. The characters of a string can be
of any kind such as letters, numbers, symbols, whitespace and punctuation. Strings are frequently
used to store, manipulate and display text.
You can assign string values to a variable as follows:

my_str = 'Hello world!'

Almost any kind of character that you can view on a computer can be represented as a string,
provided it is bound by quotation marks. For example, each of these is a string:

'abc'

''

'123'

'$%^'

'abc 123$%^'

Strings can be assigned with single or double quotation marks. The following two lines of code are
equivalent:

my_str = 'abc'

my_str = "abc"

We can convert a variable of one type to another by typecasting it. Typecasting will work if the data
types are compatible. For instance, a string containing a number can be converted to a numeric type
using the int() and float() functions:

int('3')

float('3.14')

Similarly, you can convert one numeric type to another:

float(42)

int(-0.001)

A numeric type can be converted to a string type using the str() function:

str(12345)

Here are some examples of invalid type conversions. Try to understand why these conversions would
not work. If you are not certain, run the commands in an IDE.

int('3.14')

float('abc')

int('abc')

Did you know?

Strings are made up of characters, which are themselves strings of length 1. This is interesting
because a character is the simplest form of a string, yet strings are sequences of characters. This self-
referential nature highlights the recursive nature of strings. Recursion is a commonly recurring
concept in code that you will encounter in the future.
To include the string with the single quote inside double quotes, we wrap it with the double quotes
and vice versa

“’Python’” single quotes inside double quotes

‘”Python”’ double quotes inside single quotes

“you’re”

Int number or int string can be converted to float but vice versa is not true. Also below data types
cannot be typecasted to int:

# int(character)

# int(word)

# int(sentence)

A numeric data type can be converted to string data type. This can be done using the str().

Variables of the boolean data type are used to store one of two values: True or False. Booleans are
crucial in programming because they enable us to perform all kinds of logical operations. At the
lowest level of code, a machine thinks in terms of ones and zeroes (binary). This is the same as saying
“yes” and “no” or True and False.

You can assign boolean values to a variable as follows:

a = True

b = False

You can typecast booleans to other data types and vice versa:

int(True) # output: 1

float(True) # output: 1.0

str(True) # output: ‘True’

int(False) # output: 0

float(False) # output: 0.0

str(False) # output: ‘False’

bool(1) # output: True

bool(0) # output: False

bool(-1) # output: True

Try to guess the outputs of the following lines of code:

bool('') - False

bool('Hello') - True

bool(' ') – True


bool(0.0) – False

bool(12345) – True

bool(-500) – True

bool(1.0) – True

bool(500.12) – True

Input and output are operations that allow our program to interact with other people or systems.
The simplest ways to perform input and output are by using the input() and print() functions,
respectively.

In summary, here is how we can use these functions:

user_input = input()

The value returned from the input() function is always of the string type. We can further provide a
prompt to the user to suggest the kind of input our program is looking for:

user_input = input('Please enter a number between 0 and 10: ')

We can display a value to our console using the print() statement. Each print statement displays a
value on a new line.

print(1)

print('abc')

Output:

abc

Note how the quotation marks are lost when printing a string. We can also print multiple values
using the same print statement by separating the values with commas:

print(1, 2, 3)

Output:

123

The input() always returns the string type.

In Python, None is the only instance of the type NoneType. This type is used to denote "nothing" or
the absence of a value. It is often the type returned by a function when the output is undefined or
unknown. For instance, if there is an issue when we attempt to access an external API, the object
returned is often None.

In summary, we can use None as follows:

a = None

type(a) # output: NoneType

We cannot typecast a variable to NoneType. Instead, we can just set its value to None.
We cannot typecast the none type of variable to int or float. However we can convert to str and bool.
Bool returns false

1.

State whether the following statement is true or false: “In Python, the NoneType data type can only
have one possible value, which is None." ACPDS - Fundamentals of Python - Variables and Data
Types - NoneType - question 1

 True

Correct! NoneType is a singleton, meaning there is only one object in this class, which is called None.

2.

How would you typecast a variable a to NoneType?

 a = None

Correct! Indeed, the only way to set something to type NoneType is to manually reassign it to None.

Summary:

Variables are used to store data or information and can be referenced or manipulated later in a
program. Data types specify the type of data that a variable can store. In Python, variables are
dynamically typed, meaning that the type of a variable is automatically inferred from what kind of
data it holds. This is in contrast to other languages such as C, where one must specify the data type
of a variable in advance.

Variables are important in Python because without them, we would not be able to refer to a
previously existing value. It would, therefore, be difficult for us to operate on this value or to reuse it
in any context.

Operators are of several types. In summary, the most commonly used operators in Python are as
follows:

 Arithmetic operators

 Assignment operators

 Comparison operators

 Logical operators

 Membership operators

Arithmetic operators are operators that let you perform simple mathematical operations on variables
and values. You might be familiar with most of these operators from high school mathematics,
although the symbols may vary. The operators are as follows:

 Addition: +

 Subtraction: -
 Multiplication: *

 Division: /

 Floor division (i.e. division rounded to the previous whole number): //

 Modulus (i.e. the remainder you obtain after a floor division operation): %

 Exponentiation: **

 In summary, here is what was covered in the demonstration:


 a=2
 b=3

 Addition:
 2 + 3 # output: 5
 a + b # output: 5

 Subtraction:
 2 - 3 # output: -1
 a - b # output -1

 Multiplication:
 2 * 3 # output: 6
 a * b # output: 6

 Division (note how the output of dividing two integers is a float):


 2 / 3 # output: 0.6666666666666666
 a / b # output: 0.6666666666666666

 Floor division:
 2 // 3 # output: 0
 a // b # output: 0

 Modulus:
 2 % 3 # output: 2
 a % b # output: 2

 Exponentiation:
 2 ** 3 # output: 8
 a ** b # output: 8

 After performing an operation on a variable, you can store the value in a new

variable:
 c=a**b
 print(c) # output: 8

Operators can be overloaded in Python. What this means is that an operator may have different
meanings depending on the operands involved. Usually, these are intuitive. Arithmetic operators are
primarily meant to operate on numeric data types. However, adding two strings together results in
string concatenation, while multiplying a string with an integer leads to string replication.

Operator overloading in strings:

my_str1 = 'hello'
my_str2 = 'world'

String concatenation:

my_str1 + my_str2 # output: ‘helloworld’

String replication:

my_str1 = 'hello'

my_str2 = 'world'

Note: Strings lose their quotes when printed.

print(‘Hello’, name, ‘!’)

print(‘Hello’, name + ‘!’)

bool of an empty string returns false while bool of string consisting of values will evaluate to true

months = (age – int(age)) * 12

days = (months – int(months))*30

Assignment operators are those operators that allow you to assign a value to a variable. Many of
these operators also allow you to perform arithmetic while assigning a value to a variable. The
assignment operators in Python are as follows:

 Assign: =

 Add and assign: +=

 Subtract and assign: -=

 Multiply and assign: *=

 Divide and assign: /=

 Floor divide and assign: //=

 Modulus and assign: %=

 Exponentiate and assign: **=

 The assignment operation can be done as follows:


 a=1

 b=c=2

 x, y, z = 1, 2.0, True

 Let’s perform the following operations on the variable a. Let’s first print out

what a contains.
 print(a) # output: 1

 Add and assign:


 a += 2 # equivalent is a = a + 2

 print(a) # output: 3

 Subtract and assign:


 a -= 2 # equivalent is a = a - 2

 print(a) # output: 1

 Multiply and assign:


 a *= 2 # equivalent is a = a * 2

 print(a) # output: 2

 Divide and assign:


 a /= 2 # equivalent is a = a / 2

 print(a) # output: 1.0

 Floor divide and assign:


 a //= 0.4 # equivalent is a = a // 0.4

 print(a) # output: 2.0

 Modulus and assign:


 a %= 3 # equivalent is a = a % 3

 print(a) # output: 2.0

 Exponentiate and assign:


 a **= 3 # equivalent is a = a ** 3

 print(a) # output: 8.0

A comparison operator evaluates the relationship between two operands and returns a boolean
value as the output. These operators are invaluable in decision-making and filtering data. For now,
we will restrict ourselves to understanding the outputs of these operations on different data types.
The comparison operators in Python are as follows:

 Equals: == (note the difference between this and the assignment operator =)

 Does not equal: !=

 Greater than: >

 Less than: <

 Greater than or equal to: >=

 Less than or equal to: <=

 We can compare different values to each other by using comparison

operators. The output of a comparison operation is either True or False.


 a=3

 b=4

 Equals:
 a == b # output: False

 Does not equal:


 a != b # output: True

 Greater than:
 a > b # output: False

 Less than:
 a < b # output: True

 Greater than or equal to:


 a >= b # output: False

 Less than or equal to:


 a <= b # output: True

You might also like