Posts

How do I return the position of an item in a list in Python?

Find the position of an element in a list using the  index()  method. In python ,  Index()  method returns the position of an element in a list. Note:  Indexing starts with  ‘0′  not  ‘1’. Let’s see an example of it: list = [1,2,3,7,8,5] print(list.index(3)) Output: 2 Let's see another example: What happens in that case when we find out the position of an element that is not present in the given list. list = [1,2,3,5,7] print(list.index(4)) In that case, the compiler throws an error. i.e  4 is not in the list What happens in that case, when the repeated element present in the list Example: list = [1,2,3,4,2,6,2] print(list.index(2)) Output : 1  

How do I count the number of one element in an array in Python?

Method1: count() Using the  count()  function you will get the number of one element in an array in python . It is a built-in function in python. list = [1,9,5,3,9,2,6,9,9,0,9] count = list.count(9) print('The count of element: 9 is ', count) Output: The count of element: 9 is 5   Method2:                     list = [1,2,6,6,6,8,6,6,6] count = 0 for i in list: if(i==6): count = count + 1 print('The count of element: 6 is ', count)   Output: The count of element: 6 is 6   I hope it helps you.

How to know your public and private IP addresses

You may have used the ifconfig command in your Linux terminal to understand the various network configurations on your system. The ifconfig command displays the hardware address (HWaddr) and network address (inet addr) for your Ethernet or WiFi connection. the ifconfig command does not display your public IP address (if public and private are different). To do this you can simply do a google search: what is my IP address, it will show you your public IP address. In a Linux terminal, you can use the following command to find out your IP address:     public IP: curl ifconfig.me or curl ipinfo.io/ip Private IP : hostname -I

How can we check the Python version of Linux?

How to Check Python Version in Linux Most modern Linux distributions come with Python pre-installed. To access a command line or terminal in Linux using Ctrl-Alt-T, Ctrl-Alt-F2 Just open a terminal window and enter the following: python --version How to Check Python Version in Windows Open window Powershell using win + R then type Powershell and enter Ok python --version How to Check Python Version in macOS Open a terminal using Finder > Applications > Utilities > Terminal type python --version you will get the python version

How do I add an int to the end of a sting Python?

you can add an int to the end of a string with the help of type conversion. In this example, we simply convert the int data type to string data type using type conversion and adding it to the appropriate position. Steps: step1: take a string data type. step2: take an int data type. step3: convert the int into the string using type conversion. step 4: add the string and int and assign to a variable. step5: print the variable. Example: string = "python" int = 446 res = string + str(int) print("The string after adding number is : " + str(res)) By using the %d operator: string = "python" int = 446 res = (string + "%d")%int print("The string after adding number is : " + str(res)) Output: python446

What does RES mean in Python?

"Res" or "RES" has no inherent meaning in Python . If you've seen this in any part of Python code, maybe in someone's answer to a question, then it's probably just the variable name the author chose. It's often used as an abbreviation for "result" - people sometimes do things like: Yes, "res" is an abbreviated version of "result". Sometimes variable names are so shortened that they completely lose readability.

How do I print a number with only two decimal places in Python?

You want to print a number with only two decimal places in python . print only two decimal places by using. Format method % formatting round method and etc. Let’s see an example of it. By using Format Method: num = 16.52907534 # Take a float number. print(“{:.2f}”.format(num)) By using % formatting: print("%.2f" % num) By using round method: print("{:.2f}".format(round(num,2)))   Output: 16.52 16.52 16.52