Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1345 of 3366
869 Views
Python has an inbuilt round() function to round off a number.The round() method in Python takes two parameters −The first one is the number to be rounded off.The second one specifies the number of digits to which the number must be rounded off.Here, the second parameter is optional.If second parameter is not specified, then the round() method returns the integer using floor() and ceil().It will look for the digits after the decimal.If those are less than 5, it returns the floor() of the number passed.Whereas if the digits after decimal are greater than 5, it returns the ceil() of the ... Read More
2K+ Views
Patterns in Python can be printed using nested for loops. The outer loop is used to iterate through the number of rows whereas the inner loop is used to handle the number of columns. The print statement is modified to form various patterns according to the requirement.The patterns can be star patterns, number patterns, alphabet patterns. The patterns can be of different shapes, triangle, pyramids, etc.ExampleAll these patterns can be printed with the help of for loops with modified print statements which forms these different patterns.The basic idea between the printing of these patterns is the same with slight differences.We ... Read More
2K+ Views
Reversing an integer number is an easy task. We may encounter certain scenarios where it will be required to reverse a number.Input: 12345 Output: 54321There are two ways, we can reverse a number −Convert the number into a string, reverse the string and reconvert it into an integerReverse mathematically without converting into a stringConvert into string and ReverseThis method of reversing a number is easy and doesn’t require any logic. We will simply convert the number into string and reverse it and then reconvert the reversed string into integer. We can use any suitable method for reversing the string.Example Live Demodef ... Read More
37K+ Views
Concatenating two strings refers to the merging of both the strings together. Concatenation of “Tutorials” and “Point” will result in “TutorialsPoint”.We will be discussing different methods of concatenating two strings in Python.Using '+' operatorTwo strings can be concatenated in Python by simply using the '+' operator between them.More than two strings can be concatenated using '+' operator.Example Live Demos1="Tutorials" s2="Point" s3=s1+s2 s4=s1+" "+s2 print(s3) print(s4)OutputTutorialsPoint Tutorials PointUsing % operatorWe can use the % operator to combine two string in Python. Its implementation is shown in the below example.Example Live Demos1="Tutorials" s2="Point" s3="%s %s"%(s1, s2) s4="%s%s"%(s1, s2) print(s3) print(s4)OutputTutorials Point TutorialsPointThe %s denotes ... Read More
123K+ Views
The print() method in Python automatically prints in the next line each time. The print() method by default takes the pointer to the next line.Example Live Demofor i in range(5): print(i)Output0 1 2 3 4Modify print() method to print on the same lineThe print method takes an extra parameter end=" " to keep the pointer on the same line.The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.Syntaxprint("..." , end=" ")Print on same line with space between each elementThe end=” “ is used ... Read More
68K+ Views
In Python, we need not declare a variable with some specific data type.Python has no command for declaring a variable. A variable is created when some value is assigned to it. The value assigned to a variable determines the data type of that variable.Thus, declaring a variable in Python is very simple.Just name the variableAssign the required value to itThe data type of the variable will be automatically determined from the value assigned, we need not define it explicitly.Declare an integer variableTo declare an integer variable −Name the variableAssign an integer value to itExample Live Demox=2 print(x) print(type(x))This is how you ... Read More
2K+ Views
A Dictionary in Python is a type of data structure.It consists of a collection of key-value pairs. Each key in the dictionary is unique. Each unique key in the dictionary is associated to its value. Thus, Dictionary holds key : value pairs.We will be discussing how to create a dictionary in Python.Create a DictionaryA dictionary in Python can be created by placing various key: value pairs inside curly braces .The key:value pairs are separated from each other using commas(, ). The values in the dictionary can be of any datatype and can be duplicated. However, the keys in the dictionary ... Read More
1K+ Views
Type conversion is at times needed when the user wants to convert one data type into another data type according to requirement.Python has in-built function str() to convert an integer to a string. We will be discussing various other methods in addition to this to convert int into string in Python.Using str()This is the most commonly used method to convert int into string in Python.The str() takes integer variable as a parameter and converts it into a string.Syntaxstr(integer variable)Example Live Demonum=2 print("Datatype before conversion", type(num)) num=str(num) print(num) print("Datatype after conversion", type(num))OutputDatatype before conversion 2 Datatype after conversion The type() function ... Read More
71K+ Views
The list in python is a collection of similar items. We may at times need to compare data items in the two lists to perform certain operations. We will discuss certain methods to compare two lists in python.Using list.sort() and == operatorThe list.sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list. This means that the list [1, 2, ... Read More
2K+ Views
There may be some situations, where we need to convert a list into a string. We will discuss different methods to do the same.IterationIterate through the list and append the elements to the string to convert list into a string. We will use for-in loop to iterate through the list elements.Example Live Demolist1=["Welcome", "To", "Tutorials", "Point"] string1="" for i in list1: string1=string1+i string2="" for i in list1: string2=string2+i+" " print(string1) print(string2)OutputWelcomeToTutorialsPoint Welcome To Tutorials PointUsing .join() methodThe list will be passed as a parameter inside the join method.Example Live Demolist1=["Welcome", "To", "Tutorials", "Point"] string1="" print(string1.join(list1)) string2=" " print(string2.join(list1))OutputWelcomeToTutorialsPoint Welcome To ... Read More