Home
Locate
CS 101
CS 496
Login
Tutors
Marks
About
Send
Close
Add comments:
(status displays here)
Got it!
This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.
nbsp; Note: This appears on each machine/browser from which this site is accessed.
Python: Types
by RS
admin@ycp.powersoftwo.org
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. Python: Types
Python is dynamically typed, so it can be useful to determine the type of a variable at run-time. Here is the Python code.
list1 = [ 123, 45.67, "hello", True, [ 2 , 3 , 5], { "red" : "FF0000" , "green" : "00FF00", "blue" : "0000FF" } ] for item1 in list1: type1 = type(item1) type2 = str(type1) text1 = str(item1) class1 = item1.__class__.__name__ print("\"{0:s}\" :: {1:s} :: {2:s}".format(class1,type2,text1))
Here is the output of the Python code.
"int" :: <class 'int'> :: 123 "float" :: <class 'float'> :: 45.67 "str" :: <class 'str'> :: hello "bool" :: <class 'bool'> :: True "list" :: <class 'list'> :: [2, 3, 5] "dict" :: <class 'dict'> :: {'red': 'FF0000', 'green': '00FF00', 'blue': '0000FF'}
Here is the Python code.
def say1(x1, indent1): list1 = [] class1 = x1.__class__.__name__ if class1 == "int": list1.append(str(x1)) elif class1 == "str": pass elif class1 == "bool" : pass elif class1 == "list" : pass elif class1 == "dict" : pass else: list1.append("?") text1 = "".join(list1) return text1 def say(x1): list1 = say1(x1,0) text1 = "".join(list1) return text1 print("(not ready)")
Here is the output of the Python code.
(not ready)
2. End of page
by RS
admin@ycp.powersoftwo.org