Skip to main content

Python Casting

 

Python Casting


Specify a Variable Type

There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.

Casting in python is therefore done using constructor functions:

  • int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)
  • float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)
  • str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals

Example

Integers:

x = int(1)   # x will be 1
y = int(2.8# y will be 2
z = int("3"# z will be 3

Example

Floats:

x = float(1)     # x will be 1.0
y = float(2.8)   # y will be 2.8
z = float("3")   # z will be 3.0
w = float("4.2"# w will be 4.2

Example

Strings:

x = str("s1"# x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'

Comments

Popular posts from this blog

Python Tuples

  Python  Tuples mytuple = ( "apple" ,  "banana" ,  "cherry" ) Tuple Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and  unchangeable . Tuples are written with round brackets. Example Create a Tuple: thistuple = ( "apple" ,  "banana" ,  "cherry" ) print (thistuple) Tuple Items Tuple items are ordered, unchangeable, and allow duplicate values. Tuple items are indexed, the first item has index  [0] , the second item has index  [1]  etc. Ordered When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. Unchangeable Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. Allow Duplicates Since tuples...

Python Strings

  Python  Strings  Strings Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello'  is the same as  "hello" . You can display a string literal with the  print()  function: Example print ( "Hello" ) print ( 'Hello' ) Assign String to a Variable Assigning a string to a variable is done with the variable name followed by an equal sign and the string: Example a =  "Hello" print (a) Multiline Strings You can assign a multiline string to a variable by using three quotes: Example You can use three double quotes: a =  "" "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." "" print (a) Or three single quotes: Example a =  '' 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' '' print (a) Note:  in the res...

Python Arrays

  Python  Arrays Note:  Python does not have built-in support for Arrays, but  Python Lists  can be used instead. Arrays Note:  This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the  NumPy library . Arrays are used to store multiple values in one single variable: Example Create an array containing car names: cars = [ "Ford" ,  "Volvo" ,  "BMW" ] What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: car1 =  "Ford" car2 =  "Volvo" car3 =  "BMW" However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? The solution is an array! An array can hold many values under a single name, and you can access the values by referring to an index nu...