Basic Python Data Types#

1. Number (int and float)#

The most straightfoward data type in Python, “what you see is what you get.”

x = 6  # integer
y = 1.5  # float
z = x + y  # addition
print(z)
7.5
z = x/y  # division
print(z)
4.0

2. String#

The Python string data type is a sequence made up of one or more individual characters that could consist of letters, numbers, whitespace characters, or symbols. String is defined by enclosing (single or double) quotes around texts.

print("Welcome to URP6271!")
Welcome to URP6271!
print('Welcome to GIS Automation.')
Welcome to GIS Automation.

Note

  • "" is an empty string.

  • " " is a space which is also a string.

  • A double quoted string can contain single quotes within it and vice versa.

print("I'm starting to learn Python.") 
I'm starting to learn Python.
print('Albert Einstein said: "You never fail until you stop trying."')
Albert Einstein said: "You never fail until you stop trying."

3. String Operation#

String operation is a fundamental task in Python. Here, we will look at several basic techniques.

3.1 Concatenation#

String concatenation means combining strings together. The easiest way to perform this task is to use the + character to add one string to another:

s1 = "Welcome to"
s2 = "ArcGIS Pro customization"
print(s1 + s2)
Welcome toArcGIS Pro customization
print(s1 + ' ' + s2)
Welcome to ArcGIS Pro customization

3.2 Indexing and Slicing#

Indexing means referring to an element of an iterable by its position within the iterable. Each of a string’s characters corresponds to an index number and each character can be accessed using its index number. We can access characters in a String in 2 ways :

  • Accessing Characters by Positive Index Number

  • Accessing Characters by Negative Index Number

Note

In Python, and virtually all other programming languages, index starts from 0.

print(s1)
print(s1[5])  # positive index number
print(s1[-5])  # negative index number
Welcome to
m
m

Slicing in Python is a feature that enables accessing parts of the sequence. In slicing a string, we create a substring, which is essentially a string that exists within another string. We use slicing when we require a part of the string and not the complete string.

Note

For slicing, starting (from) index is included, and end (to) index not included.

Skipping indices: - starting index: substring from beginning of the initial string - end index: substring to the end of the intial string - both: substring is exactly the same as initial string

s3 = s1 + s2
print(s3[10:16])  # to get the substring - "ArcGIS"
ArcGIS
print(s3[:7]) # skip the start index
print(s3[21:]) # skip the end index
Welcome
customization
s3[:10] + " " + s3[10:] # fix whitespace missing in the last string
'Welcome to ArcGIS Pro customization'

Tip

Slicing can take a third (optional) argument, which is called step. It determines the increment between each index for slicing. Also, this argument can be negative which can reverse the order of the original string.

s4 = "0123456789"
print(s4[1::2])
13579
print(s4[::-1])  # reverse the order of characters
9876543210

3.3 Basic String Functions: upper, lower, str#

s5 = 'abcdefghijklmnopqrstuvwxyz'
print(s5.upper())  # change to upper case
ABCDEFGHIJKLMNOPQRSTUVWXYZ
s5  # s5 is still lower case
'abcdefghijklmnopqrstuvwxyz'
s5 = s5.upper()  # reassign the capitalized value to s5
print(s5)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
s5.lower()
'abcdefghijklmnopqrstuvwxyz'

Caution

Different data types cannot be mixed in a single operation. In this situation, you need to transform them into the same data type. For example, use str() function to transform an integer to a string.

hw_name = "Changjie's Homework"
num = 1
hw_name + num
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22552\4287257492.py in <cell line: 3>()
      1 hw_name = "Changjie's Homework"
      2 num = 1
----> 3 hw_name + num

TypeError: can only concatenate str (not "int") to str
hw_name + str(num) # convert an object from other types of data to string
"Changjie's Homework1"

4. Boolean#

Booleans represent one of two values: True or False. In programming, you often need to know if an expression is true or false. You can evaluate any expression in Python, and get one of two answers, True or False. When you compare two values, the expression is evaluated and Python returns the Boolean answer:

x == 100
False

Note

= is the assignment operator. You use it to assign a value to a variable. However, == is a comparison operator. You use it to compare whether two values are equal or not.

See also

Python Operators

11 > 7
True
0 < -1
False
y != 0  # not equals
True

Python statements that make comparisons are called conditions because they will result in a Boolean value indicating whether a condition is met (True) or not (False).

Note

In Python, True equals to 1 and False equals to 0, internally.

print(True == 1)
print(False == 0)
True
True

5. List#

Lists are used to store multiple items in a single variable. Lists are created using square brackets, i.e., [].

city_list = ['Gainesville', 'Orlando', 'Miami', 'Tampa']
city_list
['Gainesville', 'Orlando', 'Miami', 'Tampa']

A list can contain more than one data types.

mixed_list = ['Gainesville', 'Orlando', 'Miami', 4, True]
print(mixed_list)
['Gainesville', 'Orlando', 'Miami', 4, True]

A list allows duplicates.

mixed_list = ['Gainesville', 'Orlando', 'Miami', 4, True, 'Orlando']
len(city_list) # return the number of elements in the list
4

Note

It follows the same rules for indexing and slicing on lists as on strings.

two_cities = city_list[0:2]
print(two_cities)
two_cities = city_list[:2]
print(two_cities)
['Gainesville', 'Orlando']
['Gainesville', 'Orlando']
print(city_list[:4])
print(city_list[-1])
print(city_list[::2])
['Gainesville', 'Orlando', 'Miami', 'Tampa']
Tampa
['Gainesville', 'Miami']

A list is mutable, i.e., allow changes to its value.

city_list[0] = 'Jacksonville'
print(city_list)
['Jacksonville', 'Orlando', 'Miami', 'Tampa']