Python Thops
Python Thops
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
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
Python Program
● 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.
Unlike the other programming languages, Python print() function is most unique and versatile
function.
● sep - The sep parameter separates the print values. Default values is ' '.
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
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
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.
>>> 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']
Example
x = 15 + 1.3
Operators
The operator is a symbol that performs a certain operation between two
operands.
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.
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.
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.
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.