Tutorials
Hands-On Python
Hands-On Python
  • Hands-On Python Tutorial For Real-World Business Analytics Problems
  • Preface
    • Section I. A Note From The Author
    • Section II. Tutorial Overview
    • Section III. What Is The Preflight Checklist?
    • Section IV. Supplimentery Material
  • Preflight Checklist
    • Section V. Select Your Difficulty Setting
    • Section VI. Download Anaconda
    • Section VII. Download PyCharm (Optional)
    • Section VIII. Download SQL Server Developer Edition
    • Section IX. Configure Database Environment
    • Section X. Download The Source Code
    • Section XI. Starting JupyterLab
    • Section XII. How To Get Help With This Tutorial
  • Language Basics
    • Lesson 1. Obligatory Hello World
    • Lesson 2. Code Comments
    • Lesson 3. Data Types
    • Lesson 4. Variables
    • Lesson 5. String Concatenation
    • Lesson 6. Arithmetic Operators
    • Lesson 7. Making Decisions
    • Lesson 8. Control Flow With if-elif-else
    • Lesson 9. Control Flow With while
    • Lesson 10. Data Structures Part I: List
    • Lesson 11. Data Structures Part II: Tuples
    • Lesson 12. Data Structures Part III: Dictionaries
    • Lesson 13. Looping With for
    • Lesson 14. Functions
    • Lesson 15. Importing Modules
    • Lesson 16. Python Programming Standards
  • Advanced Topics
    • Lesson 17. Functional Programing With map
    • Lesson 18. Generators
    • Lesson 19. Comprehensions
    • Lesson 20. Basic File Operations
    • Lesson 21. Working With Data In Numpy
    • Lesson 22. Working With Data In Pandas
    • Lesson 23. Working With JSON
    • Lesson 24. Making File Request Over HTTP And SFTP
    • Lesson 25. Interacting With Databases
    • Lesson 26. Saving Objects With Pickle
    • Lesson 27. Error Handling
    • Lesson 28. Bringing It All Together
  • Solutions To Real World Problems
    • Lesson 29. Download A Zip File Over HTTP
    • Lesson 30. Looping Over Files In A Directory
    • Lesson 31. Convert Comma Delmited Files To Pipe Delimited
    • Lesson 32. Combining Multiple CSVs Into One File
    • Lesson 33. Load Large CSVs Into Data Warehouse Staging Tables
    • Lesson 34. Efficiently Write Large Database Query Results To Disk
    • Lesson 35. Working With SFTP In The Real World
    • Lesson 36. Executing Python From SQL Server Agent
Powered by GitBook
On this page
  • Examples
  • Now you try it!
  1. Language Basics

Lesson 3. Data Types

Python is different from practically every other programming language that I have worked with in that you do not explicitly define data types. When you create a variable, you have to assign it a value. How you assign it a value will define the data type of the variable.

If you are coming from another programming language, or are familiar with OO programming in general, then you may be familiar with the idea that class and data type are synonymous. Classes are objects and everything in Python is an object. That makes discussions about data types in Python simpler than it would be in other languages.

This lesson goes over what I like to call the fundamental data types. These are the data types that you would find in all programming languages. Data types unique to Python like list and tuples are discussed in their own lessons.

String

Python stores textual data using the str object. Python has a lot of power to manipulate strings which we will be learning about in later lessons. Once you figure out how easy it is to work with string data in Python, you probably won’t want to learn any other programming language ever again.

Boolean

Every computer language gives you the power to represent true and false values. Python does it with the keywords True and False. The T and the F are capitalized and yes it matters.

Integers

Technically, Python supports every integer value between positive and negative infinity. In practice, you will be limited by your machine’s memory.

Float

Floats are how Python represents decimal values in a non-precise way. What that means is, when you are working with floats, you have to look out for things like rounding errors.

Floats have their place in computer science, but, if you are going to work in analytics precision will be required.

Decimal

This is my recommended way to deal with real numbers. You have to be careful when you create your decimal value by passing the constructor either a string or integer value. If you pass the constructor a float value, the result will still suffer from approximation issues.

Examples

Creating Strings

String variables are created by wrapping the value in quotes. You can use single or double quotes.

foo = 'This is a string value.'
foo2 = "This is also a string value."

print(foo)
print(foo2)

Creating Booleans

Booleans are both a data type and a value, so you don't have to explicitly assign a variable as a Boolean. As you can see below, when we cast these values to integers, they come out 0 and 1 just like we would expect from basic computer science.

print(int(True))
print(int(False))

Creating Integers

Creating integers is straight forward. You assign a variable a value that is a number but not a decimal and do not surround the value with quotes.

x = 1
y = 2

print(x+y)

Creating Floats

You create floats by using floating point numbers when you assign a variable a value. Below are some examples. There is also an example of how the imprecision of float can cause problems.

If you do the last calculation by hand, you should get an answer of 0. I even plugged it into my TI-84 and got 0. Because it is a float the value of b gets printed out as a non-zero number.

x = 1
y = 2.5
z = x + y

print(z)

a = 10e-2

print(a)

b = .3 - (.1 * 3)

print(b) #What is this nonsense?

Creating Decimals

An unfortunate thing about creating decimals is that you have to import a module. We will talk about modules in a later lesson, so I am going to hand wave them for now.

We are going to fix the issue of the calculation of b from above by passing the Decimal constructor strings in place of float values.

from decimal import *

b = Decimal('.3') - (Decimal('.1') * 3)

print(b) #That's more like it!

Now you try it!

Don't copy and past. Type the code yourself!

PreviousLesson 2. Code CommentsNextLesson 4. Variables

Last updated 3 years ago