Beruflich Dokumente
Kultur Dokumente
Introduction to python
Basic Python
• 1.0 Variables
• 1.1 Manipulating Variables
• 1.2 Operators
Example 2:
# Define the variable x
x=3
output:
The value of x is: 3.2
<class 'float'>
output:
6.2
<class 'float'>
Calling the function float(number) will turn an integer into a number that can
contain decimal places.
Another type of variable: Strings
A string in python is a variable that contains text. This can take many forms, and a string can
contain numbers in the form of text. Here are a couple of examples of strings.
Example 4:
# Declare a string:
# Strings can be multiple letters
string1 = 'Hello World'
# Strings can hold a text representation of a number
string2 = "1"
# Strings can hold a text representation of multiple numbers
string3 = '1 2 3'
# Print each out
print(string1)
print(string2)
print(string3)
output:
Hello World
1
123
methods
• You can find the length of a string using len()
• Turn a string into all lower case string.lower()
• The same works with .upper() method
• Split a string using the .split() method
Example 5:
# Declare the string
string1 = 'Hello World'
#This 'The length of the string is: ' lower case letters is: 'will print the string `hello world`
print('The string in all lower case letters is: ', string1.lower())
Algebra
Operations in python
• * = Multiplication
• Can be used with floats and integers
• + = Addition
Other functions:
• ** = Exponential
• // Integer Division
• In computing, the modulo operation finds the remainder after division of one number
by another (sometimes called modulus)
Example 6:
# Declare variables x and y
x=2
y=3
in python index always start at 0 and you can access any element of a list using its
index.
Manipulating Varables
Example 8:
lst = [1,2,3,4]
The in operator
Membership operators are used to test if a sequence is presented in an object: