Python Fundamentals
Python is a high-level, interpreted programming language known for its simplicity and readability. This chapter covers the fundamental concepts of Python programming that form the foundation for more advanced topics.
1. Introduction to Python
Python was created by Guido van Rossum and first released in 1991. It is an interpreted, high-level, general-purpose programming language that emphasizes code readability with its notable use of significant whitespace.
Key features of Python include:
- Simple and easy to learn syntax
- Interpreted language (no need for compilation)
- Dynamically typed (no need to declare variable types)
- Object-oriented programming support
- Extensive standard library
- Cross-platform compatibility
2. Python Installation and Setup
To start programming in Python, you need to install Python on your computer. The latest version can be downloaded from the official Python website (python.org). After installation, you can use IDLE (Integrated Development and Learning Environment) that comes with Python, or other IDEs like PyCharm, VS Code, or Jupyter Notebook.
3. Basic Syntax and First Program
Let's start with a simple "Hello, World!" program:
# This is a comment print("Hello, World!")When you run this program, it will display:
4. Variables and Data Types
In Python, you don't need to declare variables before using them. Variables are created when you assign a value to them.
# Integer age = 25 # Float height = 5.9 # String name = "John Doe" # Boolean is_student = True # Printing variables print("Name:", name) print("Age:", age) print("Height:", height) print("Is student:", is_student)Python has the following basic data types:
- Numeric Types: int, float, complex
- Text Type: str
- Boolean Type: bool
- Sequence Types: list, tuple, range
- Mapping Type: dict
- Set Types: set, frozenset
- None Type: NoneType
5. Operators
Python supports various operators for performing operations on variables and values:
- Arithmetic Operators: +, -, *, /, %, **, //
- Assignment Operators: =, +=, -=, *=, /=, %=, **=, //=
- Comparison Operators: ==, !=, >, <, >=, <=
- Logical Operators: and, or, not
- Identity Operators: is, is not
- Membership Operators: in, not in
# Arithmetic operators a = 10 b = 3 print("a + b =", a + b) # Addition print("a - b =", a - b) # Subtraction print("a * b =", a * b) # Multiplication print("a / b =", a / b) # Division print("a % b =", a % b) # Modulus print("a ** b =", a ** b) # Exponentiation print("a // b =", a // b) # Floor division6. Input and Output
Python provides built-in functions for input and output operations:
# Taking input from the user name = input("Enter your name: ") # Converting input to integer age = int(input("Enter your age: ")) # Output print("Hello,", name) print("You are", age, "years old") # Formatted output using f-strings (Python 3.6+) print(f"Hello, {name}. You are {age} years old.")Important Note
Always remember to convert the input to the appropriate data type when needed. The input() function returns a string by default, so you need to convert it to int, float, etc., if you want to perform numerical operations.
7. Type Conversion
Python provides built-in functions for converting from one data type to another:
# Type conversion x = 10 # Integer y = 3.14 # Float z = "20" # String # Convert integer to float float_x = float(x) print(float_x) # Output: 10.0 # Convert float to integer int_y = int(y) print(int_y) # Output: 3 (truncates decimal part) # Convert string to integer int_z = int(z) print(int_z) # Output: 20 # Convert to string str_x = str(x) print(str_x) # Output: "10"8. Comments in Python
Comments are used to explain the code and make it more readable. Python supports single-line and multi-line comments:
# This is a single-line comment """ This is a multi-line comment or docstring that can span multiple lines """ print("Hello, World!") # This is an inline commentPractice Questions
- Write a Python program to calculate the area of a rectangle. Take length and width as input from the user.
- Write a Python program to convert temperature from Celsius to Fahrenheit. The formula is: F = (C * 9/5) + 32
- Write a Python program to swap the values of two variables without using a temporary variable.
- Write a Python program to calculate the simple interest. The formula is: SI = (P * R * T) / 100, where P is the principal amount, R is the rate of interest, and T is the time period.
- Write a Python program to find the average of three numbers entered by the user.
Related Resources
Need Help?
Join our tuition classes for personalized guidance and doubt clearing.
Register for Classes →