Home > Backend Development > Python Tutorial > Python Essentials for JS Developers

Python Essentials for JS Developers

Barbara Streisand
Release: 2024-11-07 03:41:02
Original
1022 people have browsed it

Python Essentials for JS Developers

1. Basic Syntax and Data Types

  • Variable Declaration: No need for var, let, or const. Just name the variable.

     x = 10
     name = "Python"
    
    Copy after login
    Copy after login
  • Primitive Types:

    • int (Integer)
    • float (Floating Point)
    • str (String)
    • bool (Boolean)
  • Data Structures:

    • Lists (like arrays in JS):
       numbers = [1, 2, 3]
       numbers.append(4)
    
    Copy after login
    • Tuples (immutable lists):
       point = (10, 20)
    
    Copy after login
    • Dictionaries (like JS objects):
       person = {"name": "Alice", "age": 30}
       person["name"]  # Accessing value
    
    Copy after login
    • Sets (unique, unordered elements):
       unique_numbers = {1, 2, 3, 2}
    
    Copy after login

2. Control Structures

  • Conditionals:

     if x > 5:
         print("Greater")
     elif x == 5:
         print("Equal")
     else:
         print("Lesser")
    
    Copy after login
  • Loops:

    • For Loop (works with iterable objects):
       for num in [1, 2, 3]:
           print(num)
    
    Copy after login
    • While Loop:
       i = 0
       while i < 5:
           i += 1
    
    Copy after login

3. Functions

  • Function definition and return syntax:

     def greet(name):
         return f"Hello, {name}"
    
    Copy after login
  • Lambda Functions (like JS arrow functions):

     square = lambda x: x * x
    
    Copy after login

4. List Comprehensions and Generators

  • List Comprehensions (efficient way to create lists):

     squares = [x * x for x in range(10)]
    
    Copy after login
  • Generators (yielding values one by one):

     def generate_numbers(n):
         for i in range(n):
             yield i
    
    Copy after login

5. Error Handling

  • Try/Except Blocks:

     try:
         result = 10 / 0
     except ZeroDivisionError:
         print("Cannot divide by zero")
    
    Copy after login

6. Classes and OOP

  • Class Definition:

     class Animal:
         def __init__(self, name):
             self.name = name
    
         def speak(self):
             return f"{self.name} makes a sound"
    
    Copy after login
  • Inheritance:

     class Dog(Animal):
         def speak(self):
             return f"{self.name} barks"
    
    Copy after login

7. Common Built-In Functions

  • len(), max(), min(), sum(), sorted()
  • Type conversions: int(), float(), str(), list(), dict()

8. Working with Files

  • Reading and Writing:

     x = 10
     name = "Python"
    
    Copy after login
    Copy after login

9. Important Libraries

  • NumPy for numerical operations, Pandas for data manipulation, and Matplotlib for plotting.

10. Differences from JavaScript

  • No need for semicolons.
  • Indentation is mandatory for defining blocks.
  • No switch statement (use if-elif instead).
  • None is used instead of null.

This summary should provide the essentials to begin coding in Python efficiently.

The above is the detailed content of Python Essentials for JS Developers. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template