Posts

Showing posts with the label Python Datatypes

learnpython24-(Python Numbers, Type Conversion and Mathematics)

  Python Numbers, Type Conversion and Mathematics In this article, you'll learn about the different numbers used in Python, how to convert from one data type to the other, and the mathematical operations supported in Python. Number Data Type in Python Python supports integers, floating-point numbers and complex numbers. They are defined as  int ,  float , and  complex  classes in Python. Integers and floating points are separated by the presence or absence of a decimal point. For instance, 5 is an integer whereas 5.0 is a floating-point number. Complex numbers are written in the form,  x + yj , where  x  is the real part and  y  is the imaginary part. We can use the  type()  function to know which class a variable or a value belongs to and  isinstance()  function to check if it belongs to a particular class. Let's look at an example: a = 5 print (type(a)) print (type( 5.0 )) c = 5 + 3j print (c + 3 ) print (isi...

learnpython24-(Python List)

Image
  Python List In this article, we'll learn everything about Python lists, how they are created, slicing of a list, adding or removing elements from them and so on. Python offers a range of compound data types often referred to as sequences. List is one of the most frequently used and very versatile data types used in Python. How to create a list? In Python programming, a list is created by placing all the items (elements) inside square brackets  [] , separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.). # empty list my_list = [] # list of integers my_list = [ 1 , 2 , 3 ] # list with mixed data types my_list = [ 1 , "Hello" , 3.4 ] A list can also have another list as an item. This is called a nested list. # nested list my_list = [ "mouse" , [ 8 , 4 , 6 ], [ 'a' ]] How to access elements from a list? There are various ways in which we can access the elements of a list. List Index We ca...