0% found this document useful (0 votes)
8 views

CheatSheet Python Data Structure

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CheatSheet Python Data Structure

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Cheat Sheet: Basic Data Types

Description Example

Boolean The Boolean data type is a truth value, either ## 1. Boolean Operations
​ alse​.
True​ ​or F x, y = ​True​, ​False
print(x ​and​ ​not​ y) ​# True
The Boolean operators ordered by priority: print(​not​ x ​and​ y ​or​ x) ​# True
not​ x​ ​ → “if x is False, then x, else y”
x ​and​ y​ → “if x is False, then x, else y” ## 2. If condition evaluates to False
x ​or​ y​ ​ → “if x is False, then y, else x” if​ ​None​ ​or​ ​0​ ​or​ ​0.0​ ​or​ ​''​ ​or​ [] ​or​ {} ​or​ set():
​# None, 0, 0.0, empty strings, or empty
These comparison operators evaluate to ​True​: ​# container types are evaluated to False
1​ < ​2​ ​and​ ​0​ <= ​1​ ​and​ ​3​ > ​2​ ​and​ ​2​ >=​2​ ​and
print(​"Dead code"​) ​# Not reached
1​ == ​1​ ​and​ ​1​ != ​0​ ​# True

Integer, An integer is a positive or negative number ## 3. Arithmetic Operations


Float without floating point (e.g. ​3​). A float is a x, y = ​3​, ​2
positive or negative number with floating point print(x + y) ​# = 5
precision (e.g.​ ​3.14159265359​). print(x - y) ​# = 1
print(x * y) ​# = 6
The ‘​//​’ operator performs integer division. print(x / y) ​# = 1.5
The result is an integer value that is rounded print(x // y) ​# = 1
toward the smaller integer number print(x % y) ​# = 1s
(e.g. 3​ ​ // ​2​ == ​1​). print(-x) ​# = -3
print(abs(-x)) ​# = 3
print(int(​3.9​)) ​# = 3
print(float(​3​)) ​# = 3.0
print(x ** y) ​# = 9

String Python Strings are sequences of characters. ## 4. Indexing and Slicing


s = ​"The youngest pope was 11 years old"
The four main ways to create strings are the print(s[​0​]) ​# 'T'
following. print(s[​1​:​3​]) ​# 'he'
print(s[​-3​:​-1​]) ​# 'ol'
1. Single quotes print(s[​-3​:]) ​# 'old'
'Yes' x = s.split() ​# creates string array of words
2. Double quotes print(x[​-3​] + ​" "​ + x[​-1​] + ​" "​ + x[​2​] + ​"s"​)
"Yes"
# '11 old popes'
3. Triple quotes (multi-line)
"""Yes
## 5. Most Important String Methods
We Can"""
y = ​" This is lazy\t\n "
4. String method
print(y.strip()) ​# Remove Whitespace: 'This is lazy'
str(​5​) == ​'5'​ ​# True
print(​"DrDre"​.lower()) ​# Lowercase: 'drdre'
5. Concatenation
print(​"attention"​.upper()) ​# Uppercase: 'ATTENTION'
"Ma"​ + ​"hatma"​ ​# 'Mahatma'
print(​"smartphone"​.startswith(​"smart"​)) ​# True
print(​"smartphone"​.endswith(​"phone"​)) ​# True
print(​"another"​.find(​"other"​)) ​# Match index: 2
These are whitespace characters in strings.
print(​"cheat"​.replace(​"ch"​, ​"m"​)) ​# 'meat'
● Newline \​ n
print(​','​.join([​"F"​, ​"B"​, ​"I"​])) ​# 'F,B,I'
● Space ​ s
\
print(len(​"Rumpelstiltskin"​)) ​# String length: 15
● Tab ​ t
\ print(​"ear"​ ​in​ ​"earth"​) ​# Contains: True

You might also like