TCS 151-SM04
TCS 151-SM04
What is Language?
Language is a mode of communication that is used to share ideas, opinions with each other.
For example, if we want to teach someone, we need a language that is understandable by both
communicators.
i. Machine Language
Machine language is a type of low-level programming language. It is also called as machine
code or object code. Machine language is easier to read because it is normally displayed in
binary or hexadecimal form (base 16) form. It does not require a translator to convert the
programs because computers directly understand the machine language programs.
The advantage of machine language is that it helps the programmer to execute the programs
faster than the high-level programming language.
Assembly language (ASM) is also a type of low-level programming language that is designed
for specific processors. It represents the set of instructions in a symbolic and human-
understandable form. It uses an assembler to convert the assembly language to machine
language.
The advantage of assembly language is that it requires less memory and less execution time
to execute a program.
2. High-level programming language
The main advantage of a high-level language is that it is easy to read, write, and maintain.
High-level programming language includes Python, Java, JavaScript, PHP, C#, C++,
Objective C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.
The advantage of POP language is that it helps programmers to easily track the program flow
and code can be reused in different parts of the program.
The main advantage of object-oriented programming is that OOP is faster and easier to
execute, maintain, modify, as well as debug.
Natural language is a part of human languages such as English, Russian, German, and
Japanese. It is used by machines to understand, manipulate, and interpret human's language.
It is used by developers to perform tasks such as translation, automatic summarization,
Named Entity Recognition (NER), relationship extraction, and topic segmentation.
The main advantage of natural language is that it helps users to ask questions in any subject
and directly respond within seconds.
3. Middle-level programming language
Middle-level programming language lies between the low-level programming language and
high-level programming language. It is also known as the intermediate programming
language and pseudo-language.
A middle-level programming language's advantages are that it supports the features of high-
level programming, it is a user-friendly language, and closely related to machine language
and human language.
History of Python
Python, a high-level programming language, was created by Guido van Rossum and first
released in 1991. Its development began in the late 1980s at Centrum Wiskunde &
Informatica (CWI) in the Netherlands, where van Rossum worked. The initial goal of Python
was to create a language that was easy to read and use, yet powerful enough for complex
tasks.
Here's a brief history of Python:
Creation (Late 1980s - Early 1990s):
Guido van Rossum started working on Python in December 1989.
The first version, Python 0.9.0, was released in February 1991.
Python's design philosophy emphasized code readability and simplicity.
Python 1.0 (1994):
Python 1.0 was released in January 1994.
It included features like lambda, map, filter, and reduce functions.
Python started gaining popularity among developers for its clean syntax and ease of learning.
Python 2.x Series (2000s):
Python 2.0 was released in 2000 with features like garbage collection and Unicode support.
The Python 2.x series saw numerous updates and improvements, becoming widely adopted in
the software industry.
However, Python 2.x faced criticism due to certain design flaws, particularly related to
Unicode handling and division behavior.
Python 3.0 (2008):
Python 3.0, also known as Python 3000 or "Py3k," was a significant milestone in Python's
history.
It aimed to rectify the design flaws of Python 2.x while maintaining backward compatibility
where feasible.
Python 3.0 introduced syntax changes and removed outdated features, making the language
more consistent and robust.
Python 3.x Series (2008 - Present):
Python 3.x series has been the focus of development since 2008.
Major releases within the Python 3.x series, such as Python 3.5, 3.6, 3.7, 3.8, and 3.9, brought
new features, optimizations, and improvements.
Python 3.x adoption has been steadily increasing, and Python 2.x officially reached its end of
life in January 2020.
Features of Python Programming:
Python is a versatile and powerful programming language known for its simplicity,
readability, and flexibility. It offers a wide range of features that make it popular among
developers across various domains. Here are some key features of Python:
Simple and Readable Syntax: Python's syntax is designed to be straightforward and easy to
understand, resembling pseudo-code in many ways. Its indentation-based block structure
enhances readability and reduces the need for explicit delimiters like braces.
High-level Language: Python abstracts low-level details, allowing developers to focus more
on solving problems rather than dealing with system-level programming intricacies.
Interpreted Language: Python is an interpreted language, which means that code written in
Python is directly executed without the need for compilation. This facilitates rapid
development and prototyping.
Rich Standard Library: Python comes with a comprehensive standard library that provides
modules and packages for a wide range of tasks, including file I/O, networking, data
manipulation, web development, and more. This extensive library reduces the need for third-
party dependencies in many cases.
Platform Independence: Python is platform-independent, meaning code written in Python
can run on various operating systems like Windows, macOS, and Linux without
modification.
Object-Oriented Programming (OOP): Python supports object-oriented programming
paradigms, allowing developers to create and use classes, objects, and inheritance for code
organization and reuse.
Extensive Ecosystem: Python has a vast ecosystem of third-party libraries and frameworks
that extend its capabilities for specific domains and tasks. For example, libraries like NumPy,
Pandas, Matplotlib, and scikit-learn are widely used in data science and machine learning.
Dynamically Typed: Python is dynamically typed, which means you don't have to declare
the data type of variables explicitly. The interpreter infers the data type based on the value
assigned to the variable.
Community Support: Python has a large and active community of developers who
contribute to its growth and development. The Python community is known for its
inclusivity, helpfulness, and commitment to open-source principles.
Basic Syntax
Python syntax refers to the set of rules that defines the combinations of symbols that are
considered to be correctly structured programs in the Python language. These rules make
sure that programs written in Python should be structured and formatted, ensuring that the
Python interpreter can understand and execute them correctly.
print("Hello, World!")
Hello, World!
Python is a versatile programming language that finds applications across various domains.
Some of the prominent application areas of Python include:
Web Development: Python is widely used for web development due to frameworks like
Django and Flask. These frameworks enable developers to create powerful and scalable web
applications and APIs.
Data Science and Machine Learning: Python's extensive libraries such as NumPy, Pandas,
Matplotlib, and scikit-learn make it a popular choice for data analysis, machine learning, and
artificial intelligence projects. Libraries like TensorFlow and PyTorch are also widely used
for deep learning tasks.
Scientific Computing: Python is utilized in scientific computing and computational sciences
due to its simplicity and extensive libraries for numerical computations, simulations, and
visualization.
Automation and Scripting: Python's simplicity and readability make it an excellent choice
for automating repetitive tasks, such as system administration, file manipulation, and data
processing.
Desktop GUI Applications: Python can be used to develop desktop graphical user interface
(GUI) applications using libraries like Tkinter, PyQt, and wxPython. These libraries provide
tools for creating interactive and cross-platform desktop applications.
Game Development: Python is employed in game development for prototyping, scripting,
and building game engines. Libraries like Pygame offer tools for creating 2D games, while
other frameworks like Panda3D support the development of 3D games.
Network Programming: Python's built-in libraries, such as socket, and third-party libraries
like Twisted, enable developers to create network applications, implement networking
protocols, and build servers and clients for various network services.
DevOps and System Administration: Python is widely used in DevOps practices and
system administration tasks. Tools like Ansible, Fabric, and SaltStack leverage Python for
automation, configuration management, and deployment tasks.
Education and Learning: Python's simplicity and readability make it an ideal language for
teaching programming concepts to beginners. Many educational institutions and coding
bootcamps use Python as an introductory language for teaching programming.
Financial and Quantitative Analysis: Python is extensively used in the finance industry for
quantitative analysis, algorithmic trading, risk management, and financial modeling. Libraries
like QuantLib and Pandas are commonly employed for financial data analysis and modeling.
When you run this code, it will prompt the user to enter their name. Once they enter their
name and press Enter, it will print a greeting message with the entered name.
print() Function:
The print() function is used to display output to the console. It can accept zero or more
arguments separated by commas. Each argument is converted to a string and displayed on the
console. By default, print() ends with a newline character (\n), but you can change this
behaviour using the end parameter.
Number System
A number system, also known as a numeral system, is a mathematical notation for
representing numbers. Different number systems are used in various fields of mathematics,
computer science, and everyday life. Here are some common number systems:
1. Decimal Number System (Base-10):
The decimal number system is the most common number system used by humans. It is base-
10, meaning it uses 10 symbols (0-9) to represent numbers. Each position in a decimal
number represents a power of 10. For example, in the number 354, the digit 4 represents
4×1004×100, the digit 5 represents 5×1015×101, and the digit 3 represents 3×1023×102.
2. Binary Number System (Base-2):
The binary number system is widely used in computer science and digital electronics. It is
base-2, meaning it uses only two symbols (0 and 1) to represent numbers. Each position in a
binary number represents a power of 2. For example, in the binary number 1010, the digit 0
represents 0×200×20, the second 1 represents 1×211×21, the third 0 represents 0×220×22,
and the first 1 represents 1×231×23.
3. Octal Number System (Base-8):
The octal number system is base-8, meaning it uses eight symbols (0-7) to represent numbers.
Each position in an octal number represents a power of 8.
4. Hexadecimal Number System (Base-16):
The hexadecimal number system is base-16, meaning it uses sixteen symbols (0-9 and A-F)
to represent numbers. It is commonly used in computer science, especially for representing
binary data in a more compact and human-readable form. Each position in a hexadecimal
number represents a power of 16.
Operators in Python
In Python, operators are special symbols or keywords that perform operations on operands.
Operands are the values or variables that the operator acts upon. Python supports various
types of operators, including arithmetic operators, comparison operators, logical operators,
assignment operators, bitwise operators, and more. Let's discuss each category:
1. Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations such as addition,
subtraction, multiplication, division, modulus, and exponentiation.
Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus (Remainder): %
Exponentiation: **
Floor Division (Integer Division): //
2. Comparison Operators:
Comparison operators are used to compare the values of two operands. They return Boolean
values (True or False) based on the comparison.
Equal to: ==
Not equal to: !=
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
3. Logical Operators:
Logical operators are used to combine conditional statements. They return True or False
based on the logical condition.
Logical AND: and
Logical OR: or
Logical NOT: not
4. Assignment Operators:
Assignment operators are used to assign values to variables.
Assignment: =
Addition assignment: +=
Subtraction assignment: -=
Multiplication assignment: *=
Division assignment: /=
Modulus assignment: %=
Exponentiation assignment: **=
Floor division assignment: //=
5. Bitwise Operators:
Bitwise operators perform operations on binary representations of integers.
Bitwise AND: &
Bitwise OR: |
Bitwise XOR: ^
Bitwise NOT: ~
Left shift: <<
Right shift: >>
6. Membership Operators:
Membership operators are used to test if a sequence (such as a string, list, tuple) contains a
specific value.
in: Evaluates to True if the value is found in the sequence.
not in: Evaluates to True if the value is not found in the sequence.
7. Identity Operators:
Identity operators are used to compare the memory locations of two objects.
is: Evaluates to True if both variables point to the same object.
is not: Evaluates to True if both variables point to different objects.
Expression in Python
1. Arithmetic Expressions:
Arithmetic expressions involve arithmetic operations such as addition, subtraction,
multiplication, division, modulus, and exponentiation.
result = 5 + 3 * (2 - 1)
2. String Expressions:
String expressions involve string concatenation using the + operator or repetition using the *
operator.
greeting = "Hello, " + "world!"
3. Comparison Expressions:
Comparison expressions evaluate to a Boolean value (True or False) based on the comparison
of two values.
is_greater = 10 > 5
4. Logical Expressions:
Logical expressions involve logical operations such as AND (and), OR (or), and NOT (not).
is_valid = (age > 18) and (name != "admin")
8. List Comprehensions:
List comprehensions provide a concise way to create lists based on existing lists or other
iterable objects.
squared_numbers = [x ** 2 for x in range(1, 6)]
9. Dictionary Comprehensions:
Similar to list comprehensions, dictionary comprehensions create dictionaries based on
existing dictionaries or iterable objects.
squared_dict = {x: x ** 2 for x in range(1, 6)}