Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8141

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T10:21:07+00:00 2024-11-28T10:21:07+00:00

Tricky Python Questions

  • 60k

In the last two years, I've used Python extensively as my main programming language. Dive into these tricky Python questions, inspired by real-world issues and online challenges, to test and enhance your coding skills.

So are you ready to get your mind blow away (from Python question)?

Alt Text

Questions

Notice 1!: To each question, there is an answer with an explanation (link below each item).
Notice 2!: For each question think what will be the output.

Question 1

exapmle_dict = dict() exapmle_dict.a = "string" print(exapmle_dict) 
Enter fullscreen mode Exit fullscreen mode

Go to answer 1

Question 2

class Json:     def __init__(self, *args, **kwargs):         import json      def print_dict_as_json(self, obj):         print(json.dumps(obj))  example_json = Json() example_json.print_dict_as_json({"a": "string"}) 
Enter fullscreen mode Exit fullscreen mode

Go to answer 2

Question 3

def myFun(arg1, arg3, **kwargs):     for key, value in kwargs.items():         print("%s == %s" % (key, value))   my_dict = {'arg1':1, 'arg2': 2} myFun(**my_dict, arg3=3) 
Enter fullscreen mode Exit fullscreen mode

Go to answer 3

Question 4

def add_to_all_1(arr):     for i in range(len(arr)):         arr[i] +=1  def my_func():     arr = [1,2,3]     add_to_all_1(arr)     arr2 = arr     print(arr2)  my_func() 
Enter fullscreen mode Exit fullscreen mode

Go to answer 4

Answers

Answer To Question 1

If you said:

{"a": "string"} 
Enter fullscreen mode Exit fullscreen mode

unfortunately, you are wrong, the answer is:

AttributeError: 'dict' object has no attribute 'a' 
Enter fullscreen mode Exit fullscreen mode

If you like me and came from javascript first, the access dictionary (object in Javascript) is not by dot like in Javascript, you can access only by [], and inside the key you want to set "a".

Back to question 1

Answer To Question 2

If you said:

{"a": "string"} 
Enter fullscreen mode Exit fullscreen mode

You are wrong again, the answer is:

... NameError: name 'json' is not define 
Enter fullscreen mode Exit fullscreen mode

You may know the differences between local and global scope in Python (if not you should read this: Python Scope). The __init__ is a function, the import is inside a local scope so it doesn't know what is json. You can fix it by import it globally like this:

import json  class Json:     def print_dict_as_json(self, obj):         print(json.dumps(obj))  example_json = Json() example_json.print_dict_as_json({"a": "string"}) 
Enter fullscreen mode Exit fullscreen mode

Or in a more advanced way:

class Json:     import json as json     def print_dict_as_json(self, obj):         print(self.json.dumps(obj))  example_json = Json() example_json.print_dict_as_json({"a": "string"}) 
Enter fullscreen mode Exit fullscreen mode

You can see using import inside class for more details.

Back to question 2

Answer To Question 3

If you said:

arg2 == 2 
Enter fullscreen mode Exit fullscreen mode

You are right! In Python, we have 3 ways to pass an argument:

  • By the argument itself:
def myFun(arg1):    print(arg1)  myFun('arg1') 
Enter fullscreen mode Exit fullscreen mode

  • By *args – list or tuples of arguments ( allows us to pass a variable number of non-keyword arguments to a Python function):
def myFun(*arg1):    print(*arg1)  my_tuple = ('arg1', 'arg2') myFun(my_tuple) 
Enter fullscreen mode Exit fullscreen mode

  • By kwargs can pass key=value arguments (kwargs allows us to pass a variable number of keyword arguments to a Python function) – like in the question. If you want to read more about the subject you can read here: freecodecamp – How to Use *args and **kwargs in Python

Back to question 3

Answer To Question 4

The answer is:

[2, 3, 4] 
Enter fullscreen mode Exit fullscreen mode

For some people who know scopes and assignments, it can seem a pretty easy question. For those who don't know, python saves variable memory as a reference, so in this case, the arr will point to a reference in a memory -> the function will change the values (but still the same reference) -> arr2 will get the reference address of arr but after values were modified.

Back to question 4

Thank you for reading this article. I hope you enjoyed and learned new things. If you have any questions or suggestions, please leave a comment.

careerinterviewpythonwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.