Python data types.docx - Python data types last modified...

This preview shows page 1 - 4 out of 17 pages.

The preview shows page 2 - 4 out of 17 pages.
Python data typeslast modified July 6, 2020In this part of the Python programming tutorial, we talk aboutPython data types.Computer programs, such as spreadsheets, text editors,calculators, or chat clients, work with data. Tools to work withvarious data types are essential part of a modern computerlanguage.Python data type definitionData typeis a set of values and the allowable operations on thosevalues.Python has a great set of useful data types. Python's data typesare built in the core of the language. They are easy to use andstraightforward.Python Boolean valuesThere is a duality built in our world. There is a Heaven and Earth,water and fire, jing and jang, man and woman, love and hatred. InPython programming language, the Boolean datatype is aprimitive datatype having one of two values:TrueorFalse. This is afundamental data type. Interestingly, this data type was not therefrom the beginning, but it was created later on.Happy parents are waiting a child to be born. They have chosen aname for both possibilities. If it is going to be a boy, they mighthave chosen John. If it is going to be a girl, they might havechosen Victoria.kid.py#!/usr/bin/env python# kid.pyimport random
male = Falsemale = bool(random.randint(0, 1))if male:print("We will use name John")else:print("We will use name Victoria")The script uses a random integer generator to simulate our case.import randomHere we import the random module that is used to calculaterandom numbers.male = bool(random.randint(0, 1))Here we use two functions. therandint()function returns arandom number from the given integer boundaries. In our case 0or 1. Thebool()function converts the integers to boolean values.if male:print("We will use name John")else:print("We will use name Victoria")We print the name. Theifkeyword works with boolean values. IfthemaleisTrue, we print the "We will use name John" to theconsole. If it has aFalsevalue, we print the other string.The following script shows some common values that areconsidered to beTrueorFalse.bool_fun.py#!/usr/bin/env python# bool_fun.pyprint(bool(True))print(bool(False))print(bool("text"))print(bool(""))print(bool(' '))print(bool(0))print(bool())print(bool(3))print(bool(None))The example prints nine boolean values.
$ ./bool_fun.py
This is the output of thebool_fun.pyscript.
There is another special data type —None. This data type meansnon existent, not known, or empty.none.py#!/usr/bin/env python# none.pydef function():passprint(function())In our example, we define a function. Functions will be coveredlater in the tutorial. The function does nothing. It does notexplicitly return any value. Such a function will implicitlyreturnNone.$ ./none.pyNoneThis is the output of thenone.pyscript.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 17 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Two
Professor
N/A
Tags

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture