sylvainluenga / python_exercices Goto Github PK
View Code? Open in Web Editor NEWtsconfig.json
tsconfig.json
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']
ReactDOM.render(, document.getElementById('root'));
R) h1
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)
#init allocates memory for E1.
print(E1.name)
print(E1.age)
print(E1.salary)
===========output=============
XYZ
23
20000
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
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.
count_recursive(n + 1)
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
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)
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 #
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.