Loading notes...
Loading notes...
BCA • Chapter 1
Chapter 1 introduces Python's history, setup, and core syntax. It covers variable creation, dynamic typing, fundamental data types (int, float, str, bool), and standard I/O mechanisms using input() and print().
Welcome to the world of Python programming! Python is one of the most popular, versatile, and powerful programming languages in the world today. Whether you are interested in web development, data science, artificial intelligence, automation, or software engineering, Python is the foundational language that powers modern computing. In this chapter, we will dive deep into its history, explore why it is so beloved by developers, and learn how to write your very first Python program.
Unlike languages such as C or Java which require strict syntax and compilation steps, Python is designed to be highly readable and intuitive. It uses English-like keywords instead of complex punctuation, making it an excellent language for beginners and experts alike.
1.1 History and Features of Python
Python was conceived in the late 1980s by Guido van Rossum at the Centrum Wiskunde & Informatica (CWI) in the Netherlands. It was initially designed as a successor to the ABC programming language. The first version, Python 0.9.0, was officially released in February 1991. The name 'Python' does not come from the snake, but rather from Guido's love for the British comedy group 'Monty Python's Flying Circus'.
Over the decades, Python has evolved significantly. Python 2.0 was released in 2000, introducing list comprehensions and a garbage collection system. The major overhaul, Python 3.0, was released in 2008. It cleaned up the language to remove redundancy, though it intentionally broke backward compatibility with Python 2. Today, Python 3 is the undisputed standard.
Python boasts several key features that make it stand out:
1.2 Installing Python and Setup
Before you can start coding, you must install the Python interpreter on your computer. The official distribution can be downloaded from python.org. When installing on Windows, it is absolutely critical to check the box that says 'Add Python to PATH'. This allows you to run Python from your command prompt or terminal from any directory.
To verify that Python is installed correctly, open your terminal (Command Prompt or PowerShell) and type:
python --version
# Expected output: Python 3.x.xWhen you install Python, you also get PIP (Python Package Installer), which is used to download third-party libraries, and IDLE (Integrated Development and Learning Environment), which is Python's default, lightweight editor.
1.3 Python IDEs (IDLE, VS Code, PyCharm)
While you can write Python code in a basic text editor like Notepad, using an Integrated Development Environment (IDE) significantly boosts productivity by providing syntax highlighting, auto-completion, and debugging tools.
1.4 Writing and Running Your First Script
Let's write the classic 'Hello, World!' program. In Python, this is remarkably simple compared to languages like Java which require boilerplate class definitions.
# This is a single-line comment. It is ignored by the interpreter.
print('Hello, World!')
"""
This is a multi-line comment (docstring).
It can span multiple lines.
"""
print('Welcome to BCA Python!')To run this script: 1. Save the file as `app.py`. 2. Open your terminal. 3. Navigate to the folder where the file is saved. 4. Type `python app.py` and press Enter.
1.5 Variables and Data Types
A variable is a named location in the computer's memory used to store data. Because Python is dynamically typed, you do not need to declare the variable's type (like `int x = 5`). You simply assign a value to a name, and Python figures out the type automatically.
Variables must follow certain naming rules (Identifiers): - Must start with a letter (a-z, A-Z) or an underscore (_). - Cannot start with a number. - Can contain numbers (0-9) inside them. - Are case-sensitive (`Age` is different from `age`). - Cannot be a reserved keyword (like `if`, `else`, `class`).
Python has several fundamental built-in data types:
# Variable Assignment
age = 20 # int
price = 99.99 # float
name = "Basant" # str
is_student = True # bool
college = None # NoneType
# Python allows chained assignment
x = y = z = 10
# Python allows multiple assignment in one line
a, b, c = 1, 2.5, "BCA"
# The type() function tells you the data type of a variable
print(type(age)) # Output: <class 'int'>
print(type(is_student)) # Output: <class 'bool'>1.6 Input and Output Functions
Programs are useless if they cannot interact with the user. Python provides `input()` to receive data from the user and `print()` to display data.
The `input()` function pauses program execution and waits for the user to type something and press Enter. A CRITICAL rule to remember: `input()` ALWAYS returns a string (str), regardless of what the user types. If you need a number, you must typecast it.
# Standard input (returns a string)
user_name = input("Enter your name: ")
# Typecasting input to an integer
# If the user types '25', it is converted from str to int
age = int(input("Enter your age: "))
# Output using print()
# You can pass multiple arguments separated by commas
print("Hello", user_name, "you are", age, "years old.")
# Using f-strings (Formatted string literals - modern and preferred)
print(f"Next year, {user_name} will be {age + 1} years old.")The `print()` function also takes special arguments like `sep` (separator) and `end`. By default, `print()` separates arguments with a space and ends with a newline (`\n`).
# Changing the default separator and end characters
print("A", "B", "C", sep="-") # Output: A-B-C
print("Loading", end="...") # Does not move to a new line
print("Done!") # Output: Loading...Done!