Docsity

Python Data Types and Structures: A Comprehensive Guide for Beginners, Slides of Programming Languages

Partial preview of the text

Download Python Data Types and Structures: A Comprehensive Guide for Beginners and more Slides Programming Languages in PDF only on Docsity!

Python Data Types and Structures

  • We shall recap the basic of Python and its corresponding libraries before proceed further
  • The followings are data types and structures to be discussed:

Python Basic Data Types

  • Python is a dynamically typed language à Python interpreter infers object type at runtime.
  • Compiled languages (e.g., C) is a statically typed à object type must be specified before compile time.

Integer

  • Arithmetic operations on integers are also easy to implement:

Floats

  • The last expression returns the mathematically correct results of 0.25 à the next basic data type, the float
  • Adding a dot to an integer value, like 1. or 1.0 causes Python to interpret the object as a float
  • Expressions involving a float also return a float object in general

Floats

  • Float objects internally represented in binary format. à decimal numbers between 0 and 1 represented as a series of the form à large or infinite series can lead to inaccuracies.
  • Fixed number of bits used for representation.
  • Some numbers can be represented perfectly with finite bits
  • But some don’t, say for à you won’t get

Floats

  • Precision depends on the number of bits used.
  • Most platforms use IEEE 754 double-precision standard (64 bits) for internal representation. à this provides 15-digit relative accuracy.
  • The accuracies is crucial for various finance applications.
  • Ensuring exact or best possible representation is sometimes necessary.
  • Representation errors in large sets may lead to significant deviations from benchmark values.

Booleans

  • Evaluating a comparison or logical expression à e.g. 4 > 3, (4 > 3) or (3 > 2) yields one or True or False as output
  • True and False are of data type bool, standing for Boolean value
  • The following code show results in bool objects

Booleans

  • One major application of Boolean value is to control the code flow via other Python keywords, such as if or while

Strings

  • Text represented using the str data type in Python.
  • str objects have helpful built-in methods.
  • Python is well-suited for working with texts and text files.
  • str objects defined using single or double quotation marks.
  • str() function used to convert other objects to strings.

Strings

  • Regarding the built-in methods, à you can, for example, capitalize the first word in the object, split the words, search for a word, etc.

Strings

  • Selected string methods

Printing String Replacements

  • Printing str object or string representation is usually accomplished by the print() function:

Printing String Replacements

  • More examples

Printing String Replacements

  • More examples