Sunday, 27 May 2018

Python Programming: A Beginner’s Tutorial

Standard
Image result for python
Introduction

Python is a versatile, beginner-friendly programming language known for its readability and wide range of applications. This tutorial is designed for absolute beginners, providing a step-by-step approach to learning Python from the ground up. It covers fundamental concepts, object-oriented programming (OOP), and practical examples with explanations.


1. Installing Python

Before starting, ensure Python is installed on your system. Download it from Python’s official website.

To verify installation, run the following command in your terminal or command prompt:

python --version

2. Writing Your First Python Program

Python uses simple syntax. Let’s start with a basic "Hello, World!" program:

print("Hello, World!")

Output:

Hello, World!

3. Python Basics

3.1 Variables and Data Types

Python supports various data types:

x = 10          # Integer
y = 3.14        # Float
name = "Alice"  # String
is_student = True  # Boolean

3.2 Type Checking and Conversion

print(type(x))  # Output: <class 'int'>
y = str(x)      # Convert int to string
print(type(y))  # Output: <class 'str'>

3.3 Taking User Input

name = input("Enter your name: ")
print("Hello, " + name)

4. Operators in Python

Python supports various operators:

# Arithmetic Operators
print(5 + 3)  # Addition
print(5 - 3)  # Subtraction
print(5 * 3)  # Multiplication
print(5 / 3)  # Division
print(5 % 3)  # Modulus
print(5 ** 3) # Exponentiation

5. Control Flow Statements

5.1 Conditional Statements (if-elif-else)

age = int(input("Enter your age: "))
if age >= 18:
    print("You are an adult.")
elif age > 12:
    print("You are a teenager.")
else:
    print("You are a child.")

5.2 Loops in Python

While Loop

count = 1
while count <= 5:
    print("Count:", count)
    count += 1

For Loop

for i in range(1, 6):
    print("Iteration:", i)

Loop with Break and Continue

for i in range(10):
    if i == 5:
        break  # Exit loop when i is 5
    if i % 2 == 0:
        continue  # Skip even numbers
    print(i)

6. Functions in Python

Functions help in code reuse and modularity.

def greet(name):
    print("Hello, " + name + "!")

greet("Alice")

Output:

Hello, Alice!

7. Lists and Tuples

7.1 Lists (Mutable, Ordered Collection)

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)

7.2 Tuples (Immutable, Ordered Collection)

coordinates = (10, 20)
print(coordinates[0])

8. Dictionaries in Python

Dictionaries store data in key-value pairs.

person = {"name": "Alice", "age": 25}
print(person["name"])

9. Object-Oriented Programming (OOP) in Python

9.1 Creating a Class and Object

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

p1 = Person("Alice", 25)
p1.greet()

9.2 Inheritance

class Student(Person):
    def __init__(self, name, age, grade):
        super().__init__(name, age)
        self.grade = grade

s1 = Student("Bob", 20, "A")
print(s1.name, s1.age, s1.grade)

9.3 Encapsulation

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # Private variable
    
    def get_balance(self):
        return self.__balance

acc = BankAccount(1000)
print(acc.get_balance())  # 1000

10. File Handling in Python

10.1 Writing to a File

with open("example.txt", "w") as file:
    file.write("Hello, Python!")

10.2 Reading from a File

with open("example.txt", "r") as file:
    content = file.read()
print(content)

Conclusion

This tutorial covered Python fundamentals, control structures, functions, OOP concepts, and file handling. By practicing these examples, beginners can gain confidence and build real-world applications using Python.


PDF Books Link to Download;


http://marvin.cs.uidaho.edu/Teaching/CS515/pythonTutorial.pdf


Location: DLF Farms, New Delhi, Delhi, India

0 comments:

Post a Comment