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”

Primitive Types:

int (Integer)

float (Floating Point)

str (String)

bool (Boolean)

Data …


This content originally appeared on DEV Community and was authored by minoblue

1. Basic Syntax and Data Types

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

     x = 10
     name = "Python"
    
  • 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)
    
    • Tuples (immutable lists):
       point = (10, 20)
    
    • Dictionaries (like JS objects):
       person = {"name": "Alice", "age": 30}
       person["name"]  # Accessing value
    
    • Sets (unique, unordered elements):
       unique_numbers = {1, 2, 3, 2}
    

2. Control Structures

  • Conditionals:

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

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

3. Functions

  • Function definition and return syntax:

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

     square = lambda x: x * x
    

4. List Comprehensions and Generators

  • List Comprehensions (efficient way to create lists):

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

     def generate_numbers(n):
         for i in range(n):
             yield i
    

5. Error Handling

  • Try/Except Blocks:

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

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"
    
  • Inheritance:

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

7. Common Built-In Functions

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

8. Working with Files

  • Reading and Writing:

     with open("file.txt", "r") as file:
         data = file.read()
    

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.


This content originally appeared on DEV Community and was authored by minoblue


Print Share Comment Cite Upload Translate Updates
APA

minoblue | Sciencx (2024-11-06T03:13:57+00:00) Python Essentials for JS Developers. Retrieved from https://www.scien.cx/2024/11/06/python-essentials-for-js-developers/

MLA
" » Python Essentials for JS Developers." minoblue | Sciencx - Wednesday November 6, 2024, https://www.scien.cx/2024/11/06/python-essentials-for-js-developers/
HARVARD
minoblue | Sciencx Wednesday November 6, 2024 » Python Essentials for JS Developers., viewed ,<https://www.scien.cx/2024/11/06/python-essentials-for-js-developers/>
VANCOUVER
minoblue | Sciencx - » Python Essentials for JS Developers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/06/python-essentials-for-js-developers/
CHICAGO
" » Python Essentials for JS Developers." minoblue | Sciencx - Accessed . https://www.scien.cx/2024/11/06/python-essentials-for-js-developers/
IEEE
" » Python Essentials for JS Developers." minoblue | Sciencx [Online]. Available: https://www.scien.cx/2024/11/06/python-essentials-for-js-developers/. [Accessed: ]
rf:citation
» Python Essentials for JS Developers | minoblue | Sciencx | https://www.scien.cx/2024/11/06/python-essentials-for-js-developers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.