Python - Numbers
Python - Numbers
Python - Numbers
int
float
complex
Variables of numeric types are created when you assign a value to them:
x = 1 # int
y = 2.8 # float
z = 1j # complex
x=1
y = 2.8
z = 1j <class 'int'>
<class 'float'>
print(type(x)) <class 'complex'>
print(type(y))
print(type(z))
Integer
x=1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Float
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z)
Float can also be scientific numbers with an "e" to
indicate the power of 10.
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Complex
Complex numbers are written with a "j" as the imaginary part:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion
4
Method Description
getstate() Returns the current internal state of the random number generator
print("Hello")
print('Hello')
Assign String to a Variable
a = "Hello"
print(a)
Multiline Strings
Can assign a multiline string to a variable by using three quotes:
Can use three double quotes:
a = "Hello, World!"
print(a[1])
Looping Through a String
for x in "banana":
print(x)
String Length
a = "Hello, World!"
print(len(a))
Check String
Example
b = "Hello, World!"
print(b[2:5])
Note: The first character has index 0.
Slice From the Start
By leaving out the start index, the range will start at the first
character:
Example
Get the characters from the start to position 5 (not
included):
b = "Hello, World!"
print(b[:5])
Slice To the End
By leaving out the end index, the range will go to the end:
Example
Get the characters from position 2, and all the way to the end:
b = "Hello, World!"
print(b[2:])
Negative Indexing
Use negative indexes to start the slice from the end of the
string:
Example
Get the characters: From: "o" in "World!" (position -5)
To, but not included: "d" in "World!" (position -2):
b = "Hello, World!"
print(b[-5:-2])
Modify Strings
Python has a set of built-in The strip() method removes any
methods that you can use on whitespace from the beginning
strings. or the end:
The upper() method returns the a = " Hello, World! "
string in upper case: print(a.strip()) # returns "Hello,
a = "Hello, World!" World!"
print(a.upper())
The replace() method replaces
The lower() method returns the a string with another string:
string in lower case: a = "Hello, World!"
a = "Hello, World!" print(a.replace("H", "J"))
print(a.lower())
Split String
The split() method returns a list where the text between the
specified separator becomes the list items.
The split() method splits the string into substrings if it finds
instances of the separator:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
String Concatenation
To concatenate, or combine, two strings you can use the +
operator. Merge variable a with variable b into variable c:
a = "Hello"
b = "World"
c=a+b
print(c)
To add a space between them, add a " ":
a = "Hello"
b = "World"
c=a+""+b
print(c)
Format - Strings
we can combine strings and numbers by using the format()
method!
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
The format() method takes can use index numbers {0} to
unlimited number of be sure the arguments are
arguments, and are placed into placed in the correct
the respective placeholders: placeholders:
Example Example
quantity = 3 quantity = 3
itemno = 567 itemno = 567
price = 49.95 price = 49.95
myorder = "I want {} pieces of myorder = "I want to pay {2}
item {} for {} dollars." dollars for {0} pieces of item {1}."
print(myorder.format(quantity, print(myorder.format(quantity,
itemno, price)) itemno, price))
Escape Characters
Code Result