Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 31 of 2547
Add the element in a Python list with help of indexing
A Python list is a collection data type that is ordered and changeable. It allows duplicate members and is the most frequently used collection data type in Python programs. We can add elements to a list at specific positions using indexing and the insert() method. Before adding elements, let's first understand how to access list elements using indexing. Accessing List Elements Using Index Every element in a list is associated with an index, which keeps the elements ordered. We can access elements by their index positions using slicing or direct indexing ? vowels_list = ['a', ...
Read MoreAdd similar value multiple times in a Python list
There are occasions when we need to add the same value multiple times to a Python list. This is useful for initializing lists, creating test data, or padding sequences. Python provides several built-in methods to achieve this efficiently. Using the * Operator This is the most straightforward method. The * operator creates a new list by repeating elements a specified number of times. Example Creating a list with repeated strings ? given_value = 'Hello! ' repeated_list = [given_value] * 5 print(repeated_list) The output of the above code is ? ['Hello! ...
Read MoreEvent scheduler in Python
Python provides the schedule module for running tasks at specific times. This module offers a simple and intuitive way to create scheduled jobs using the every() function with various time intervals. Installation First, install the schedule module ? pip install schedule Syntax schedule.every(n).[timeframe] Where: n is the time interval (integer) timeframe can be seconds, minutes, hours, days, or weekday names (monday, tuesday, etc.) Basic Example Here's a simple example that prints messages at different intervals ? import schedule import time def job(): ...
Read MoreAdding value to sublists in Python
Sometimes we need to add new values to sublists in an existing list. In this article, we will see how new values can be inserted into sublists by combining them with each item of the existing list. Using List Comprehension with Fixed-Length Sublists When you have sublists of the same length, you can use list comprehension to unpack and add new values ? data = [[10, 20], [14, 8], ['Mon', 'Tue']] print("Given List:") print(data) s = "Rise" t = "fast" result = [[m, n, s, t] for m, n in data] print("New List:") print(result) ...
Read MoreTurtle graphics using Python
Python's Turtle graphics library provides an easy way to create drawings and animations. After importing turtle, you can use commands like forward(), backward(), right(), and left() to move the turtle cursor and draw shapes. By combining these commands, you can create beautiful graphics ranging from simple shapes to complex patterns. Simple Turtle Commands forward(10) − Moves the turtle forward by 10 pixels backward(5) − Moves the turtle backward by 5 pixels right(35) − Turns the turtle clockwise by 35 degrees left(55) − Turns the turtle counter-clockwise by 55 degrees goto(x, y) − Moves the turtle to position ...
Read MorePython - Get the Index of first element greater than K
When working with Python lists, you often need to find the index of the first element that satisfies a specific condition. In this article, we'll explore different methods to get the index of the first element greater than a given value K. Using Enumeration with next() The enumerate() function provides both the index and value of each element. Combined with next(), it returns the first index where the condition is met ? numbers = [21, 10, 24, 40.5, 11] K = 25 print("Given list:", numbers) # Using next() + enumerate() result = next(k for k, ...
Read MorePython - Get sum of tuples having same first value
Tuples are Python collections that are ordered but unchangeable. When working with a list of tuples where multiple tuples share the same first element, we often need to sum the second elements of tuples that have matching first elements. Using Dictionary and For Loop This approach converts tuples to a dictionary where first elements become keys and second elements are summed as values − # List of tuples with some duplicate first elements data = [(3, 19), (7, 31), (7, 50), (1, 25.5), (1, 12)] # Create dictionary with first elements as keys, initialize to ...
Read MorePython - geometry method in Tkinter
Python's Tkinter library provides the geometry() method to control the size and position of GUI windows. This method is essential for creating properly sized and positioned applications. Syntax window.geometry("widthxheight+x_offset+y_offset") Where: width and height define window dimensions in pixels x_offset and y_offset set position on screen (optional) Basic Window Sizing Here's how to create a window with specific dimensions ? from tkinter import * base = Tk() base.geometry('200x200') stud = Button(base, text='Tutorialspoint', font=('Courier', 14, 'bold')) stud.pack(side=TOP, pady=6) mainloop() This creates a 200x200 pixel window with a button ...
Read MorePython - Check if two lists have any element in common
When working with Python lists, we often need to check if two lists share any common elements. This is useful for data comparison, finding overlaps, or validating data consistency. Python provides several efficient approaches to solve this problem. Using the 'in' Operator with Loops The most straightforward approach uses nested loops to check each element of the first list against all elements in the second list ? # Sample lists for comparison fruits = ['apple', 'banana', 'orange', 'mango'] colors = ['red', 'yellow', 'orange', 'blue'] numbers = [1, 2, 3, 4, 5] def has_common_elements(list1, list2): ...
Read Morehtml5lib and lxml parsers in Python
html5lib is a pure-python library for parsing HTML. It is designed to conform to the WHATWG HTML specification, as is implemented by all major web browsers. It can parse almost all the elements of an HTML doc, breaking it down into different tags and pieces which can be filtered out for various use cases. It parses the text the same way as done by the major browsers. It can also tackle broken HTML tags and add some necessary tags to complete the structure. lxml is also a similar parser but driven by XML features than HTML. It has dependency ...
Read More