0% found this document useful (0 votes)
21 views18 pages

Python Thops

Uploaded by

manasamarachapu
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)
21 views18 pages

Python Thops

Uploaded by

manasamarachapu
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/ 18

PYTHON

● Python is a popular general-purpose programming language.


● Python is a simple, general purpose, high level, powerful and very popular programming
language.
● Guido Van Rossum is known as the founder of Python programming. (1991)
● Python is simple and easy to learn and provides lots of high-level data structures.
● Python programming language (latest Python 3) is being used in web development, Machine
Learning applications, along with all cutting-edge technology in Software Industry.
● Python language is being used by almost all tech-giant companies like – Google, Amazon,
Facebook, Instagram, Dropbox, Uber… etc.

The biggest strength of Python is huge collection of standard library which can be used for
the following:
● Machine Learning
● Artificial Intelligence
● Data Science
● GUI Applications (like Kivy, Tkinter, PyQt etc. )
● Web frameworks like Django (used by YouTube, Instagram, Dropbox)
● Image processing (like OpenCV, Pillow)
● Web scraping (like Scrapy, BeautifulSoup, Selenium)
● Test frameworks
● Multimedia
● Scientific computing
● Computer Vision or Image Processing Applications.
● Text processing and many more..
● Desktop & Mobile Applications

Why Learn Python?

● Easy to use and Learn


● Expressive Language
● Interpreted Language
● Object-Oriented Language
● Open Source Language
● Extensible
● Integrated
● Embeddable
● Dynamic Memory Allocation
Java vs Python Program

Unlike the other programming languages, Python provides the facility to execute the code using few
lines. For example - Suppose we want to print the "Hello World" program in Java; it will take three lines
to print it.

Java Program

1. public class HelloWorld


2. {
3. public static void main(String[] args)
4. {
5. System.out.println("Hello World");
6. }
7. }

Python Program

On the other hand, we can do this using one statement in Python.

● print("Hello World")

Both programs will print the same result, but it takes only one statement without using a semicolon or
curly braces in Python.

Python print() Function


Here, The print() function displays the given object to the standard output device (screen) or to the text
stream file.

Unlike the other programming languages, Python print() function is most unique and versatile
function.

The syntax of print() function is given below.

· print(*objects, sep=” “, end='\n')

Let's explain its parameters one by one.


● objects - An object is nothing but a statement that to be printed. The * sign represents

that there can be multiple statements.

● sep - The sep parameter separates the print values. Default values is ' '.

● end - The end is printed at last in the statement.

Sample Program

1. a = 10
2. print("a =", a) # Two objects are passed in print() function

O/p :- a = 10

==============================================================

Identifiers
● Python Identifier is the name we give to a variable, function, class, module or
other object to identify it during the execution of the program.
● That means whenever we want to give an entity a name, that's called identifier.
● Sometimes variable and identifier are often misunderstood as same but they are
not.

Python Variable
● Variable is a name that is used to refer to memory location. Python variable is
also known as an identifier and used to hold value.
● Python Variable is a container that stores values.
● Python is “dynamically typed” language. Unlike other programming languages,
We do not need to declare variables before using them or declare their type.
● Python has no command for declaring a variable.
● A variable is created the moment we first assign a value to it. This means that
when you create a variable you reserve some space in the memory.
● The equal sign (=) is used to assign values to variables.

Example

x = 4 # x is of type int

x = "Sally" # x is now of type str

print(x)

Note :
● An Example of a Variable in Python is a representational name that serves as a
pointer to an object. Once an object is assigned to a variable, it can be referred
to by that name.
● The value stored in a variable can be changed during program execution.
● A Variables in Python is only a name given to a memory location, all the
operations done on the variable effects that memory location.
# valid variable name

thops = 1

Thops = 2

Th_o_ps = 5

_thops = 6

thops_ = 7

_THOPS_ = 8

print(thops, Thops, Th_o_ps)

print(_thops, thops_, _THOPS_)

Output:
1 2 5
6 7 8

Keywords
● Keywords are reserved words that have special meaning to the Python
Interpreter.
● Keywords can not be used as a variable name, function name, or any other
identifier.
● Keywords reserved with defined meanings and functions that we can only apply
for those functions.
● You'll never need to import any keyword into your program because they're
permanently present.

Here's a list of all keywords in Python Programming


The above keywords may get altered in different versions of Python. Some extra might
get added or some might be removed. You can always get the list of keywords in your
current version by typing the following in the prompt.

>>> import keyword

>>> print(keyword.kwlist)

Output :
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await',
'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while',
'with', 'yield']

An EXPRESSION is a combination of operators and operands that is interpreted to


produce some other value.

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

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

Example

x = 15 + 1.3

here, = + are operators and x, 15 & 1.3 are operands

Operators
The operator is a symbol that performs a certain operation between two
operands.

Types of Python Operators


Here's a list of different types of Python operators that we will learn in this tutorial.

1. Arithmetic operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Special Operators

Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication,etc.

Sample Program
Comparison Operators
Comparison operators compare two values/variables and return a boolean result: True
or False.

Sample Program
Logical Operators
Logical operators are used to check whether an expression is True or False. They are
used in decision-making.

Sample Program
Assignment Operators
Assignment operators are used to assign values to variables.
Sample Program
Special Operators

Bitwise Operators
Datatypes

Datatype is a classification that specifies the Python interpreter how the programmer
intends to use the data (what type of value a variable has and what type of operations
can be applied without causing an error.

Data types specify the different sizes and values that can be stored in the variable.

● Python is “dynamically typed” language. Unlike other programming languages,


We do not need to declare variables before using them or declare their type.
● Python has no command for declaring a variable.
● A variable is created the moment we first assign a value to it. This means that
when you create a variable you reserve some space in the memory.
● The equal sign (=) is used to assign values to variables.
Type Casting

The conversion of one data type into the other data type is known as type casting in
python or type conversion in python.

There may be times when you want to specify a type on to a variable. This can be done
with casting. Python is an object-orientated language, and as such it uses classes to
define data types, including its primitive types.

Python supports a wide variety of functions or methods like: int(), float(), str(), ord(),
hex(), oct(), tuple(), set(), list(), dict(), etc. for the type casting in python.

There are two types of type conversion in Python.

● Implicit Conversion - automatic type conversion


● Explicit Conversion - manual type conversion
Python Implicit Type Conversion
Python automatically converts one data type to another. This is known as implicit type
conversion.

Example 1: Converting integer to float


Let's see an example where Python promotes the conversion of the lower data type
(integer) to the higher data type (float) to avoid data loss.

In the above example, we have created two variables: integer_number and


float_number of int and float type respectively.

Then we added these two variables and stored the result in new_number.

As we can see new_number has value 124.23 and is of the float data type.

It is because Python always converts smaller data types to larger data types to avoid
the loss of data.
Explicit Type Conversion
In Explicit Type Conversion, users convert the data type of an object to required data
type.

We use the built-in functions like int(), float(), str(), etc to perform explicit type
conversion.

This type of conversion is also called typecasting because the user casts (changes) the
data type of the objects.

Example 2: Addition of string and integer Using Explicit Conversion


In the above example, we have created two variables: num_string and num_integer with
str and int type values respectively. Notice the code,

num_string = int(num_string)

Here, we have used int() to perform explicit type conversion of num_string to integer
type.

After converting num_string to an integer value, Python is able to add these two
variables.

Finally, we got the num_sum value i.e 35 and data type to be int.

In Type Casting, loss of data may occur as we enforce the object to a specific data type.

You might also like