Unit 1 - PROGRAMMING WITH
PYTHON
[BCO 081B]
Department of Computer Science
JECRC University
Some Facts about Python
● Widely Used – Python is currently the most popular multi-purpose,
high-level programming language.
● Programming Paradigms – It supports both Object-Oriented and
Procedural programming styles.
● Concise Code – Python programs are generally shorter compared to
other languages like Java.
● Less Typing Effort – Programmers need to write fewer lines of code.
● Readability – Python enforces indentation, which makes programs clean,
structured, and easy to read.
● Industry Adoption – Python is widely used by leading tech giants such as
Google, Amazon, Facebook, Instagram, Dropbox, and Uber.
Strength of Python - Standard Library
The biggest strength of Python is its huge collection of standard libraries,
which can be used for:
1. Machine Learning – Libraries like TensorFlow, scikit-learn, PyTorch, etc.
2. GUI Applications – Frameworks such as Kivy, Tkinter, PyQt.
3. Web Frameworks – For example, Django (used by YouTube, Instagram,
Dropbox).
4. Image Processing – Libraries like OpenCV, Pillow.
5. Web Scraping – Tools such as Scrapy, BeautifulSoup, Selenium.
6. Test Frameworks – For software testing and automation.
7. Multimedia – Libraries to handle audio, video, and graphics.
8. Scientific Computing – Libraries like NumPy, SciPy, and Matplotlib.
9. Text Processing – Tools for working with text, NLP, and string operations.
10. Many More – Python’s ecosystem covers almost every domain of
computing.
History of Python
1. Created in 1989 by Guido van Rossum.
2. Originally developed as a scripting language for administrative
tasks.
3. Based on earlier languages like ABC and Modula-3.
4. Designed with extensibility in mind.
5. Name Origin – Inspired by the British comedy group Monty
Python.
6. First Public Release – in 1991.
7. Attracted a growing community of developers worldwide.
8. Over time, it evolved into a well-supported, full-featured
programming language.
History of Python - Modules
1. Modules are reusable pieces of software in Python.
2. They can be written by any Python developer.
3. Modules help in extending Python’s capabilities.
4. The official Python website is [Link]
5. It serves as the primary distribution center for:
Python source code, Modules, Documentation
History of Python - Design Features
1. Designed to be portable and extensible.
2. Originally implemented on UNIX.
3. Python programs can often be ported across different operating
systems without modification.
4. Its syntax and design encourage good programming practices.
5. Enables rapid development times compared to many other languages.
6. Simple enough for beginner programmers to learn easily.
7. Powerful enough to be widely adopted by professional developers.
Notable Features of Python
1. Easy to Learn – Simple syntax and readability make it beginner-friendly.
2. Supports Quick Development – Enables rapid prototyping and faster
coding.
3. Cross-Platform – Runs on multiple operating systems (Windows, macOS,
Linux, etc.).
4. Open Source – Freely available for use and modification
5. Extensible – Can integrate with other languages like C, C++, or Java.
6. Embeddable – Python code can be embedded within other applications.
7. Large Standard Library & Active Community – Rich set of modules and
strong global developer support.
8. Versatile Applications – Useful in web development, data science, AI,
automation, scientific computing, and more.
Setting Up Python on Windows
1. Visit the official Python website: [Link].
2. Download the latest distribution (example: version 2.4.1)
3. Explore online tutorials to get started.
4. Refer to Python-related websites for additional help and
resources.
5. During installation, use all the default settings.
Python IDLE
Your First Python Program
1. At the Python prompt (>>>), type the command:
print("Game Over")
2. Press [Enter] to execute.
3. The message Game Over will be displayed on the screen.
4. Very straightforward – even without prior Python knowledge, you could
have guessed it!
The Game Over Program
Your First Python Program - Case Sensitivity
1. Python is case-sensitive, which means uppercase and lowercase letters
are treated differently.
2. Example:
✅ print("Game Over") → Works correctly.
❌ Print("Game Over") → Error (undefined name).
❌ PRINT("Game Over") → Error (undefined name).
3. Always use lowercase print when displaying output.
Syntax Errors in Python
1. A syntax error occurs when the computer does not recognize the
statement to be executed.
2. It is similar to a misspelling in a programming language.
3. Syntax errors are often referred to as bugs.
Example:
>>> primt("Game Over")
SyntaxError: invalid syntax
4. In the above example, the keyword print is misspelled as primt, causing
the error.
Programming in Script Mode
1. Interactive Mode
● Provides immediate feedback for commands.
● Not designed to create programs that can be saved and run later.
2. Script Mode
● Allows you to write, edit, save, and run programs later.
● Works like a word processor for your code.
● Files must be saved with the .py extension. (e.g., [Link]).
Program Documentation in Python
1. Comment lines provide documentation about your program.
2. Anything written after the # symbol is treated as a comment.
3. Comments are ignored by the computer during execution.
4. Example:
# New Python Programmer
# First Python Program
# at JECRC University
Data Types in Python - Numeric Types
1. Numeric Types in Python include:
● int → Integer numbers (e.g., 5, -12)
● long → Long integers (Python 2 only; in Python 3, int handles long
values too)
● float → Decimal numbers (e.g., 3.14, -2.5)
● complex → Complex numbers (e.g., 2 + 3j)
2. All numeric types, except complex, support the usual numeric
operations (addition, subtraction, multiplication, division, etc.).
3. Mixed arithmetic is supported - when two different numeric types
are used together, the “narrower” type is automatically converted
(widened) to match the other.
● Example: 5 + 3.2 → 8.2 (integer is widened to float).
4. The same rule applies for mixed comparisons.
Data Types - Examples in Python
•$ python
>>> 3 + 2 (Basic Arithmetic)
5
>>> 18 % 5 (Modulus Operator (remainder))
3
>>> abs(-7) (Absolute Value)
7
>>> float(9) (Type Conversion - Integer to Float)
9.0
>>> int(5.3) (Type Conversion – Float to Integer)
5
>>> complex(1,2) (Complex Numbers)
(1+2j)
>>> 2 ** 8 (Exponentiation)
256
Working with Numbers in Python
1. Two main numeric types:
● Integers (int) → Whole numbers (e.g., -3, -2, 0, 100123).
● Floating-point numbers (float) → Numbers with decimals (e.g., -23.2,
3.14159).
2. Key Difference:
● Integers and floats may behave differently in certain operations (e.g.,
division).
Working With Numbers
Working With Numbers
1. Parentheses first → Expressions inside () are evaluated before
anything else.
2. Exponentiation (**) → Evaluated right to left.
3. Multiplication (*), Division (/), Modulus (%) → Evaluated left to
right.
4. Addition (+) and Subtraction (-) → Evaluated left to right.
Data Types - equence Types: Strings
1. Strings are a type of sequence in Python.
2. They are created by enclosing characters in either:
Single quotes → 'Hello'
Double quotes → "Hello"
3. Example:
mystring = "Hi, I'm a string!"
Concatenating Strings in Python
1. String concatenation means joining two or more strings into a single
string.
2. The plus sign (+) is used as the concatenation operator.
3. Example:
print("Hello, " + "how " + "are " + "you")
Output:
Hello, how are you
Quotes and Strings in Python
1. Python provides two options for creating string.
● Using double quotes (" ")
● Using single quotes (' ')
2. A string is simply a sequence of characters surrounded by either of
these quotes.
3. Example: print("Program 'Game Over' 2.0")
4. Note: Quotes can be placed inside other quotes (e.g., single quotes
inside double quotes).
Escape Sequences with Strings in Python
1. An escape sequence is a special sequence of characters that provides extra
functionality in displayed text.
2. Escape sequences begin with a backslash (\).
3. Common Escape Sequences:
\\ → Prints one backslash
\' → Prints one single quote
\" → Prints one double quote
\a → Sounds the system bell (alert)
\b → Moves the cursor back one space (backspace)
\n → Moves the cursor to the beginning of the next line (newline)
\t → Moves the cursor forward one tab stop (horizontal tab)
Repeating Strings in Python
1. Strings in Python can be repeated, meaning multiple copies of the same
string are concatenated together.
2. The multiplication sign (*) is used as the repetition operator.
3. Example:
>>> "Are We There Yet? " * 3
Output:
Are We There Yet? Are We There Yet? Are We There Yet?
Understanding Variables in Python
1. In the earlier examples, programs have not used memory
storage.
● They involved direct printing of text.
● Or direct evaluation of expressions.
2. To work with memory, we need variables.
3. A variable is a name that refers to a value stored in memory.
4. Variables allow us to store, reuse, and manipulate data during
program execution.
Understanding Variables in Python
1. A variable is a named location in memory that can hold a value
of a given type.
2. Memory (RAM) can be thought of as a series of mailboxes:
● Each mailbox has a unique address.
● Each mailbox can hold a value.
3. A variable acts as a label for one of these mailboxes, making it
easier to store and access values.
Understanding Variables - Creating Variables
1. Variables must be created before use.
2. This is done using an assignment statement.
3. The assignment operator (=) is used to assign a value to a
variable
4. Examples:
x = 10
name = "Sarah"
● Here, x stores the integer value 10.
● name stores the string value "Sarah".
Understanding Variables - Using Variables
1. Once a variable is created, it refers to a specific value in memory.
2. A variable can be used just like a value in expressions or
statements.
3. Example:
name = "Sarah"
print(name)
Output :
Sarah
Understanding Variables - Naming Variables
1. Legal Variable Names
● Can only contain letters, numbers, and underscores (_).
● Cannot start with a number.
● Examples: age, student_name, score1 ✅
● Invalid: 1score, student-name, total$ ❌
2. Good Variable Names
● Choose descriptive names (e.g., marks, user_age).
● Be consistent in style.
● Follow the traditions of the language (e.g., snake_case in
Python).
● Keep the length reasonable (not too short, not too long).
Getting User Input in Python
1. To interact with the user, we need a way to get input.
2. Python provides the built-in function input().
3. By default, input() always returns the user’s input as a string.
4. If another type (e.g., integer, float) is required, the input must be
converted using type conversion functions (int(), float(), etc.).
Example :
name = input("Please enter your name: ")
print("Hello,", name)
Output :
Please enter your name: Sarah
Hello, Sarah
Converting Values in Python
1. String values can be converted to integers using the int() function.
2. This is useful because input() always returns a string by default.
3. Examples:
# Converting a string to integer
x = int("10") # x = 10
# Taking user input and converting later
y = input("Enter your age: ")
y = int(y) # Convert string input to integer
# Converting directly while taking input
z = int(input("Enter your age: "))
4. This ensures values can be used in numeric operations (e.g., addition, subtraction).
Converting Values - Float Conversion in Python
1. String values can be converted to floating-point numbers using the float()
function.
2. This is commonly used when dealing with decimal values like prices, interest rates,
or measurements.
3. Example :
# Converting a string to float
rate = float("14.5") # rate = 14.5
# Taking user input and converting later
y = input("Enter interest rate: ")
y = float(y) # Convert string input to float
# Converting directly while taking input
z = float(input("Enter interest rate: "))
4. This allows the program to perform mathematical operations with decimal
values.
Converting Values - Numbers to Strings in Python
1. Integers and floats can be converted to string values using the str() function.
2. This is useful when combining numbers with text, or when joining multiple numbers
together.
3. Example:
first4 = 1234
second4 = 5678
third4 = 2468
fourth4 = 3579
card_number = str(first4) + str(second4) + str(third4) + str(fourth4)
print(card_number)
Output: 1234567824683579
4. Here, each number is converted to a string and then concatenated.
Assignment Operators
1. An augmented assignment operator is a combination of:
● An assignment (=), and
● A mathematical operation (like +, -, *, /, etc.).
2. They provide a shorter way to update the value of a variable.
3. Examples:
x = 10
x += 5 # Same as x = x + 5 → x = 15
x -= 3 # Same as x = x - 3 → x = 12
x *= 2 # Same as x = x * 2 → x = 24
x /= 4 # Same as x = x / 4 → x = 6.0
x %= 5 # Same as x = x % 5 → x = 1.0
4. These operators make code shorter and more readable.