Code Monkey home page Code Monkey logo

python_exercices's Issues

Python (Programming Language)

How can you randomize the items of a list in place in Python?
Ans: Consider the example shown below:
from random import shuffle
x = ['Keep', 'The', 'Blue', 'Flag', 'Flying', 'High']
shuffle(x)
print(x)
=============output============
['Flying', 'Keep', 'Blue', 'High', 'The', 'Flag']

React.js

Q) If you created a component called Dish and rendered it to the DOM, what type of element would be rendered?

function Dish() { return

Mac and Cheese

; }

ReactDOM.render(, document.getElementById('root'));

R) h1

Python (Programming Language)

What is init?
Ans: init is a method or constructor in Python. This method is automatically called to allocate memory when a new object/ instance of a class is created. All classes have the init method.

Here is an example of how to use it.
class Employee:
def init(self, name, age,salary):
self.name = name
self.age = age
self.salary = 20000
E1 = Employee("XYZ", 23, 20000)

E1 is the instance of class Employee.

#init allocates memory for E1.
print(E1.name)
print(E1.age)
print(E1.salary)

===========output=============
XYZ

23

20000

Python (Programming Language)

What are docstrings in Python?
Ans: Docstrings are not actually comments, but, they are documentation strings. These docstrings are within triple quotes. They are not assigned to any variable and therefore, at times, serve the purpose of comments as well.

Example:
"""
Using docstring as a comment.
This code divides 2 numbers
"""
x=8
y=4
z=x/y
print(z)

=================output===========
2.0

Python (Programming language )

Difference Between .py files and .pyc files.

โ .py files contain the source code of a program. Whereas, .pyc file contains the bytecode of your
program.

React.js

Q) What property do you need to add to the Suspense component in order to display a spinner or loading state?

function MyComponent() { return (
); }

A) fallback

Python ( Programming Language)

Q)What would this recursive function print if it is called with no parameters?

def count_recursive(n=1): if n > 3: return print(n)
count_recursive(n + 1)
A) 1 2 3

Python (Programming Language)

Q) What will this code fragment return?
import numpy as np np.ones([1,2,3,4,5])
A) It returns a 5-dimensional array of size 1x2x3x4x5 filled with 1s

Python (Programming Language)

What is a lambda function?
Ans: An anonymous function is known as a lambda function. This function can have any number of parameters but, can have just one statement.

Example:
a = lambda x,y : x+y
print(a(5, 6))

===============output===========
11

Python (Programming Language)

In the Python file, write a program to perform a GET request on the route https://coderbyte.com/api/challenges/json/age-counting which contains a data key and the value is a string which contains items in the format: key=STRING, age=INTEGER. Your goal is to count how many items exist that have an age equal to or greater than 50, and print this final value. Example Input {"data":"key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47"}

==============code============
total = 0
initial_value = "key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47"
for item in initial_value.split(","):
item_parts = item.strip().split("=") # strip removes any space
if item_parts[0] == "age" and int(item_parts[1]) >= 50:
total = total + 1
print(total)

Python (Programming Language)

How do you write comments in python?
Ans: Comments in Python start with a # character. However, alternatively at times, commenting is done using docstrings(strings enclosed within triple quotes)

#Comments in Python start like this
print("Comments in Python start with a #")

=================output=============
Comments in Python start with a #

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.