Skip to content

Latest commit

 

History

History
282 lines (214 loc) · 9.82 KB

File metadata and controls

282 lines (214 loc) · 9.82 KB

Programming Logic form Zero to Hero

Summary

Programming logic is essential for developing software applications and solving complex problems. By mastering the fundamental concepts of programming logic, you'll be better equipped to write efficient, maintainable, and scalable code.

Learning Objectives

By the end of this section, you should be able to:

  • Define and use variables to store and manipulate data.
  • Identify and work with different data types.
  • Apply arithmetic, comparison, and logical operators to perform operations on data.
  • Use control structures like loops and conditionals to control the flow of a program.
  • Create and call functions to encapsulate and reuse code.
  • Implement error handling techniques to handle exceptions in a program.

Prerequisites

  • Desire to learn.
  • Wanna have the power to create anything you want.

Introduction

  • What is a program?
    A program is a set of instructions that tell a computer what to do. These instructions are written in a programming language, which is a formal language that can be understood and executed by a computer. Programs can perform a wide range of tasks, from simple calculations to complex data processing and analysis.
graph LR
    A[Program] --> B(Instructions)
    B --> C{Computer}
    C -->|Execute| D[Output]
Loading
  • What is programming language?
    A programming language is a formal language that can be used to write instructions that can be executed by a computer. There are many different programming languages, each with its own syntax and semantics. Some popular programming languages include Python, Java, C++, and JavaScript.
print("Hello, world!")
  • What is a formal language?
    A formal language is a language that is designed for a specific purpose and has a precise syntax and semantics. Formal languages are used in many different fields, including mathematics, logic, and computer science. Programming languages are a type of formal language that is used to write instructions that can be executed by a computer.
graph LR
    A[Formal Language] --> B(Syntax)
    A --> C(Semantics)
Loading
  • What is syntax and semantics?

    • Syntax: The rules that define the structure of a language, including the order and arrangement of symbols and words. Syntax errors occur when the rules of a language are violated.
    • Semantics: The meaning of the symbols and words in a language. Semantics errors occur when the meaning of an instruction is unclear or ambiguous.
    # Syntax error
    print("Hello, world!) # Missing closing quote
    
    # Semantics error
    func sum(a, b): # Missing return statement
        a + b
    
    # Correct code
    def sum(a, b):
        return a + b
  • Types of programming languages:

    • Low-level languages: These languages are close to machine code and are specific to a particular type of computer architecture. Examples include assembly language and machine code.
    • High-level languages: These languages are closer to human language and are easier to read and write. Examples include Python, Java, and C++.

    Types of programming languages

    # Python
    a = 1
    b = 2
    c = a + b
    // C++
    int a = 1;
    int b = 2;
    int c = a + b;
    ; Assembly language
    mov eax, 1
    mov ebx, 2
    add eax, ebx
    01010100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01110011 01110100 01110010 01101001 01101110 01100111
    

Programming objectives

  • Efficiency: Write code that is easy to read, understand, and maintain.
  • Scalability: Design programs that can handle large amounts of data and complex operations.
  • Reusability: Create code that can be reused in different parts of a program or in other programs.
  • Reliability: Develop software that is robust and error-free, with proper error handling and testing.

What is programming logic?

Programming logic is the process of creating a sequence of instructions to solve a problem. It involves breaking down a problem into smaller, more manageable parts and then designing a solution using programming constructs like loops, conditionals, and functions.

  • Properties of programming logic:

    • Sequential: Instructions are executed in order, one after the other.
    • Repetitive: Loops are used to repeat a sequence of instructions multiple times.
    • Conditional: Conditionals are used to execute different instructions based on a condition.
    • Modular: Functions are used to encapsulate and reuse code in a program.
  • Problem-solving process:

    1. Understand the problem: Identify the requirements and constraints of the problem.
    2. Break down the problem: Divide the problem into smaller, more manageable parts.
    3. Design a solution: Create a sequence of instructions to solve each part of the problem.
    4. Implement the solution: Write code that implements the instructions in a programming language.
    5. Test the solution: Verify that the code works correctly and produces the expected results.
  • Example:

    • Problem: Calculate the sum of the first n natural numbers.

    • Solution:

      1. Understand the problem: Given a positive integer n, calculate the sum of the first n natural numbers.
      2. Break down the problem: Calculate the sum of the first n natural numbers using the formula sum = n * (n + 1) / 2.
      3. Design a solution: Write a function that takes an integer n as input and returns the sum of the first n natural numbers.
      4. Implement the solution:
      def sum_of_natural_numbers(n):
          return n * (n + 1) // 2
      1. Test the solution:
      print(sum_of_natural_numbers(10)) # Output: 55
  • Programming constructs:

    • Variables: Containers for storing data that can be referenced and manipulated in a program.

      x = 10
      y = 20
      z = x + y
    • Data Types: The different types of data that can be used in a program, such as integers, floats, strings, and booleans. Data types

      a = None # None
      b = 10 # Integer from 2147483648 through 2147483647
      c = 3.14 # Float from 2.2250738585072014e-308 through 1.7976931348623157e+308
      d = 1 + 2j # Complex from 0 through 2^64-1
      e = "Hello, world!" # String, Unicode, 8-bit
      f = True # Boolean (True or False)
      g = [1, 2, 3] # List (Array)
      h = {"name": "Alice", "age": 30} # Dictionary (Object)
      i = (1, 2, 3) # Tuple (Immutable list)
      j = {1, 2, 3} # Set (Unique elements)
    • Operators: Symbols that perform operations on data, such as arithmetic, comparison, and logical operations.

      # Arithmetic operators
      x = 10 + 5 # Addition
      y = 10 - 5 # Subtraction
      z = 10 * 5 # Multiplication
      w = 10 / 5 # Division
      v = 10 % 5 # Modulus
      u = 10 ** 5 # Exponentiation
      t = 10 // 5 # Floor division
      
      # Comparison operators
      a = 10 == 5 # Equal to
      b = 10 != 5 # Not equal to
      c = 10 > 5 # Greater than
      d = 10 < 5 # Less than
      e = 10 >= 5 # Greater than or equal to
      f = 10 <= 5 # Less than or equal to
      
      # Logical operators
      g = True and False # Logical AND
      h = True or False # Logical OR
      i = not True # Logical NOT
    • Control Structures: Constructs that control the flow of a program, such as loops and conditionals.

      # Conditional statements
      if x > 0:
          print("Positive")
      elif x < 0:
          print("Negative")
      else:
          print("Zero")
      
      # Loops
      for i in range(10):
          print(i)
      
      while x < 10:
          print(x)
          x += 1
    • Functions: Blocks of reusable code that can be called with different arguments to perform specific tasks.

      def greet(name):
          return f"Hello, {name}!"
      
      print(greet("Alice")) # Output: Hello, Alice!
    • Error Handling: Techniques for handling errors and exceptions that may occur during program execution.

      try:
          x = 10 / 0
      except ZeroDivisionError as e:
          print("Error:", e)
      finally:
          print("Done")

By understanding these concepts, you'll be able to write more efficient and effective programs that can solve a wide range of problems.

Pseudo code

Pseudo code is a high-level description of a computer program or algorithm that uses natural language and informal syntax to represent the logic of the program. It is used to plan and design a program before writing the actual code.

  • Advantages of pseudo code:

    • Simplicity: Pseudo code is easy to read and understand, even for non-programmers.
    • Flexibility: Pseudo code is not tied to a specific programming language, so it can be used to plan programs in any language.
    • Clarity: Pseudo code helps to clarify the logic of a program and identify potential issues before writing the actual code.
  • Example:

    • Problem: Calculate the sum of the first n natural numbers.

    • Pseudo code:

      function sum_of_natural_numbers(n)
          sum = 0
          for i = 1 to n
              sum = sum + i
          return sum
      
    • Implementation:

      def sum_of_natural_numbers(n):
          sum = 0
          for i in range(1, n + 1):
              sum += i
          return sum
      
      print(sum_of_natural_numbers(10)) # Output: 55

Projects

Resources

Quiz

Test your knowledge of programming logic with this quiz.

Exercises

Practice what you've learned with these exercises.