DEV Community

devneagu
devneagu

Posted on

[How to] JS to Python - Core Concepts

Variables

To create a variable in Python, you use the assignment operator (=) to specify the name of the variable and the value you want to assign to it. For example, to create a variable named "message" with the value "Hello, world!", you would use the following code:

message = "Hello, world!"
Enter fullscreen mode Exit fullscreen mode

Once you have created the variable, you can use it in your program by referencing its name. For example, you could print the value of the "message" variable using the following code:

print(message)
Enter fullscreen mode Exit fullscreen mode

This would output "Hello, world!" to the screen. You can also use the variable in other expressions and statements, such as assigning it to a new variable, or using it as part of a condition in an if statement. For example:

new_message = message + " How are you?"

if message == "Hello, world!":
    print("The message is correct.")
Enter fullscreen mode Exit fullscreen mode

Can I use types in python ?

To add types to a variable in Python, you use a colon (:) after the variable name, followed by the type you want to assign to the variable. For example, to create a variable named "message" with the type "str", you could use the following code:

message: str = "Hello, world!"
Enter fullscreen mode Exit fullscreen mode

In this case, the type annotation indicates that the "message" variable should be a string (str). If you try to assign a value of a different type to the variable, you will get a type error. For example, the following code would generate an error:

message: str = 10
Enter fullscreen mode Exit fullscreen mode

Because the value 10 is not a string, this code would generate a type error when you try to run it.

Type annotations can be useful for catching errors and making your code more readable, but they are not required in Python. You can still create variables without specifying their types, and the Python interpreter will automatically infer the type based on the value you assign to the variable. For example, the following code would be valid in Python, even though it does not include type annotations:

How can I use json objects ?

In Python, objects are created from classes, which are templates or blueprints for creating objects. An object is a specific instance of a class, and it contains the data and behavior defined by the class.

To use objects in Python, you first need to define a class that specifies the structure and behavior of the objects you want to create. Here is an example of a simple class that defines a "Person" object:

class Person:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

    def say_hello(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")
Enter fullscreen mode Exit fullscreen mode

This class defines a Person object with two attributes (name and age) and one method (say_hello). The init method is a special method that is called when an object is created, and it is used to initialize the object's attributes. The say_hello method is a regular method that is called on an object to perform some action.

Once you have defined a class, you can create objects from it by calling the class like a function and passing the required arguments. For example, to create a Person object with the name "Alice" and the age 20, you could use the following code:

alice = Person("Alice", 20)
Enter fullscreen mode Exit fullscreen mode

This code creates a new Person object and assigns it to the variable "alice". You can then use the object to access its attributes and call its methods. For example:

print(alice.name)  # Output: "Alice"
print(alice.age)   # Output: 20
alice.say_hello()  # Output: "Hello, my name is Alice and I am 20 years old."
Enter fullscreen mode Exit fullscreen mode

Objects are a powerful concept in Python, and they are used extensively in object-oriented programming (OOP) to represent real-world entities in a program. OOP allows you to create modular, reusable, and extensible code, which can make your programs more efficient and easier to maintain.

What are lambdas?

A lambda is a short, anonymous function that can be defined and used inline in your code.

To create a lambda in Python, you use the lambda keyword followed by one or more arguments, a colon (:), and the expression or statements that make up the body of the lambda. Here is an example of a simple lambda that takes a single argument and returns the square of that argument:

square = lambda x: x ** 2
Enter fullscreen mode Exit fullscreen mode

In this example, the lambda takes a single argument "x" and returns the value of "x" squared. You can then use the lambda just like a regular function, by calling it with the required arguments and storing the result in a variable. For example:

result = square(10)  # result is 100
Enter fullscreen mode Exit fullscreen mode

Lambdas can be used in many different situations in Python, such as in a list comprehension or as the key function in a sorting operation. Here is an example of using a lambda in a list comprehension to square a list of numbers:

numbers = [1, 2, 3, 4, 5]
squared = [square(x) for x in numbers]
# squared is [1, 4, 9, 16, 25]
Enter fullscreen mode Exit fullscreen mode

Lambdas are a useful feature of Python, and they can help you write more concise and readable code. However, they should be used with caution, because they can make your code less readable if used excessively or in complex situations.

Functions

To write a function in Python, you use the def keyword followed by the name of the function and the arguments it takes, and then provide the statements that make up the body of the function. Here is an example of a simple function that takes a single argument and returns the square of that argument:

def square(x):
    return x ** 2

# Once you have defined a function, you can use it in your code by calling it with the required arguments.
# For example:
result = square(10)  # result is 100
Enter fullscreen mode Exit fullscreen mode

Loops

Here is an example of a for loop, which is used to iterate over a sequence of data and execute a block of code for each element in the sequence:

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    print(number)
Enter fullscreen mode Exit fullscreen mode

In this example, the code iterates over the elements in the "numbers" list, and for each element, it prints the value of that element to the screen.

Here is an example of a while loop, which is used to execute a block of code repeatedly until a specific condition is met:

x = 10

while x > 0:
    print(x)
    x -= 1
Enter fullscreen mode Exit fullscreen mode

In conclusion, Python provides the following advantages :

  • Readability: Python uses a simple and consistent syntax, and it emphasizes readability and clarity in the code.
  • Beginner-friendly: Python is designed to be beginner-friendly, and it includes many features and libraries that make it easier for beginners to get started with programming.
  • Versatility: Python is a general-purpose language that can be used for many different types of projects, from web development and data analysis to scientific computing and artificial intelligence.
  • Ease of use: Python has a simple and intuitive syntax, and it includes many built-in features and libraries that make it easy to perform common tasks without having to write a lot of code.

Top comments (0)