Code Monkey home page Code Monkey logo

se-assignment-6-introduction-to-python-estherngwasi's Introduction

Review Assignment Due Date Open in Visual Studio Code

SE-Assignment-6

Assignment: Introduction to Python Instructions: Answer the following questions based on your understanding of Python programming. Provide detailed explanations and examples where appropriate.

Questions:

  1. Python Basics:

    • What is Python, and what are some of its key features that make it popular among developers? Provide examples of use cases where Python is particularly effective.

    • Python is a high-level, interpreted programming language known for its simplicity and readability.

    • FEATURES OF PYTHON THAT MAKE IT POPULAR Readable and Maintainable Code: Python's syntax emphasizes readability, making it easier to understand and write clean code. Wide Standard Library: Includes modules for various tasks such as web development, data analysis, machine learning, etc. Dynamic Typing: No need to declare variable types explicitly. Interpreted Language: Executes code line by line, which makes debugging easier. Cross-Platform: Runs on various operating systems such as Windows, macOS, and Linux. USE CASES WHERE PYTHON IS PARTICULARY EFFECTIVE Web Development: Using frameworks like Django and Flask. Data Analysis and Visualization: Using libraries like Pandas, NumPy, and Matplotlib. Machine Learning and AI: Using TensorFlow, Keras, and Scikit-learn.

  2. Installing Python:

    • Describe the steps to install Python on your operating system (Windows, macOS, or Linux). Include how to verify the installation and set up a virtual environment.

      STEPS IN INSTALLING PYTHON WINDOWS: Download the installer from the official Python website. Run the installer and ensure you check "Add Python to PATH". Follow the installation prompts. MACOS: Install Homebrew if you don't have it: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" Install Python: brew install python Linux: Update package list: sudo apt update Install Python: sudo apt install python3

VERIFY INSTALLATION Open a terminal or command prompt and type: python --version

SETTING UP A VIRTUAL ENVIRONMENT Install virtualenv; pip install virtualenv Create a virtual environment: python -m venv myenv Activate the virtual environment: Windows: myenv\Scripts\activate macOS/Linux: source myenv/bin/activate

  1. Python Syntax and Semantics:
    • Write a simple Python program that prints "Hello, World!" to the console. Explain the basic syntax elements used in the program.

    • PYTHON PROGRAM THAT PRINTS HELLO WORLD print("Hello, World!")

EXPLANATION OF THE SYNTAX USED print: A built-in function to output text to the console. "Hello, World!": A string enclosed in double quotes. Single quotes can also be used.

  1. Data Types and Variables:
    • List and describe the basic data types in Python. Write a short script that demonstrates how to create and use variables of different data types.

      DATA TYPES IN PYTHON int: Integer numbers. float: Floating-point numbers. str: Strings. bool: Boolean values (True or False). list: Ordered collection of items. tuple: Ordered, immutable collection of items. dict: Key-value pairs. SCRIPT ON HOW TO CREATE AND USE VARIALES OF DIFFERENT DATA TYPES

Integer

age = 24 print("Age:", age)

Float

height = 5.6 print("Height:", height)

String

name = "Esther" print("Name:", name)

Boolean

is_graduate = True print("Is student:", is_student)

List

colors = ["red", "green", "blue"] print("Colors:", colors)

Tuple

coordinates = (10.0, 20.0) print("Coordinates:", coordinates)

Dictionary

person = {"name": "Esther", "age": 24} print("Person:", person)

  1. Control Structures:
    • Explain the use of conditional statements and loops in Python. Provide examples of an if-else statement and a for loop.
    • Conditional Statements: Used to perform different actions based on different conditions. Example: x = 10 if x > 0: print("x is positive") else: print("x is non-positive")
  • Loops: Used to execute a block of code repeatedly.

For Loop Example: for i in range(4): print(i)

  1. Functions in Python:
    • What are functions in Python, and why are they useful? Write a Python function that takes two arguments and returns their sum. Include an example of how to call this function.

-Functions: Blocks of reusable code that perform a specific task. They help in organizing code, making it more modular, readable, and maintainable. Example: def add_numbers(x, y): return x + y

Calling the function

result = add_numbers(3, 5) print("Sum:", result)

  1. Lists and Dictionaries:
    • Describe the differences between lists and dictionaries in Python. Write a script that creates a list of numbers and a dictionary with some key-value pairs, then demonstrates basic operations on both.
    • DEFINATION Lists:Ordered collection of items. Indexed by position (starting from 0). Dictionaries:Collection of key-value pairs. Indexed by keys (unique). SCRIPT

List

numbers = [1,2,3,4,5] numbers.append(6) print("Numbers:", numbers) print("First number:", numbers[0])

Dictionary

person = {"name": "Esther", "age": 24} person["city"] = "Nairobi" print("Person:", person) print("Name:", person["name"])

  1. Exception Handling:

    • What is exception handling in Python? Provide an example of how to use try, except, and finally blocks to handle errors in a Python script.
    • Exception Handling: A mechanism to handle runtime errors, allowing the program to continue execution or fail gracefully. Example: try: num = int(input("Enter a number: ")) result = 10 / num print("Result:", result) except ZeroDivisionError: print("Error: Division by zero is not allowed.") except ValueError: print("Error: Invalid input. Please enter a number.") finally: print("Execution completed.")
  2. Modules and Packages:

    • Explain the concepts of modules and packages in Python. How can you import and use a module in your script? Provide an example using the math module. Modules: Files containing Python code (functions, classes, variables).

    Packages: Directories containing multiple modules and a special init.py file.

Example: import math

print("Square root of 25 is:", math.sqrt(25)) print("Pi value is:", math.pi)

  1. File I/O:
    • How do you read from and write to files in Python? Write a script that reads the content of a file and prints it to the console, and another script that writes a list of strings to a file.

      Reading from a File: with open('example.txt', 'r') as file: content = file.read() print(content)

      Writing to a File: lines = ["Hello, World!", "Python is great!", "File I/O is easy."] with open('output.txt', 'w') as file: for line in lines: file.write(line + '\n')

Submission Guidelines:

  • Your answers should be well-structured, concise, and to the point.
  • Provide code snippets or complete scripts where applicable.
  • Cite any references or sources you use in your answers.
  • Submit your completed assignment by [due date].

se-assignment-6-introduction-to-python-estherngwasi's People

Contributors

github-classroom[bot] avatar estherngwasi avatar

Watchers

Eddy Angano Lugaye avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.