You are on page 1of 33

Introduction to Python

• Data Types And Operators


• Data Types: Integers, Floats, Booleans, Strings
• Operators: Arithmetic, Assignment, Comparison, Logical
• Built-In Functions, Type Conversion
• Whitespace and Style Guidelines
Arithmetic Operators
• Arithmetic operators

• + Addition
• - Subtraction
• * Multiplication
• / Division
• % Mod (the remainder after dividing)
• ** Exponentiation (note that ^ does not do this operation, as you might have
seen in other languages)
• // Divides and rounds down to the nearest integer
Example

• print(3 + 5) # 8
• print(1 + 2 + 3 * 3) # 12
• print(3 ** 2) # 9
• print(9 % 2) # 1
Variables and Assignment Operators

• Variables I
• Variables are used all the time in Python! Below is the example :
• mv_population = 74728
• Here mv_population is a variable, which holds the value of 74728.
• This assigns the item on the right to the name on the left, which is
actually a little different than mathematical equality, as 74728 does
not hold the value of mv_population.
• In any case, whatever term is on the left side, is now a name for
whatever value is on the right side. Once a value has been assigned to
a variable name, you can access the value from the variable name.
Variables II

• You can see that the following two are equivalent in terms of
assignment:

• x=3
• y=4
•z=5
• And
• x, y, z = 3, 4, 5
• variable names should be descriptive of the values they hold.
• Besides writing variable names that are descriptive, there
are a few things to watch out for when naming variables in
Python.
• 1. Only use ordinary letters, numbers and underscores in your variable
names. They can’t have spaces, and need to start with a letter or
underscore.
• 2. You can’t use Python's reserved words, or "keywords," as variable
names.
• There are reserved words in every programming language that have
important purposes,
• Creating names that are descriptive of the values often will help you
avoid using any of these keywords.
• Here you can see a table of Python's reserved words.
https://docs.python.org/3/reference/lexical_analysis.html#keywords
• 3. The pythonic way to name variables is to use all lowercase letters
and underscores to separate words.
• my_height = 58
• my_lat = 40
• my_long = 105
• Wrong Way
• my height = 58
• MYLONG = 40
• MyLat = 105
• The way we name variables is called snake case, because we tend to
connect the words with underscores.
• mv_population = 74728
mv_population = 74728 + 4000 - 600
print(mv_population) # 78128
Assignment Operators

• You can also use *= in a similar way, but this is less common than the
operations shown below.
Integers and Floats

• here are two Python data types that could be used for
numeric values:
• int - for integer values
• float - for decimal or floating point values
• You can create a value that follows the data type by using
the following syntax:
• x = int(4.7) # x is now an integer 4
• y = float(4) # y is now a float of 4.0
• You can check the type by using the type function:
• >>> print(type(x))
• int
• >>> print(type(y))
• float
• Because the float, or approximation, for 0.1 is actually slightly more
than 0.1, when we add several of them together we can see the
difference between the mathematically correct answer and the one
that Python creates.
• Good
• print(4 + 5)

• Bad
• print( 4 + 5)

• You should limit each line of code to 80 characters,


though 99 is okay for certain use cases.
Boolean
• x = 42 > 43 # False

• age = 14
• is_teen = age > 12 and age < 20
• print(is_teen) # ?
• age = 14
• is_teen = age > 12 and age < 20
• print(is_teen) # True
Booleans, Comparison Operators, and Logical Operators

• The bool data type holds one of the values True or False, which are
often encoded as 1 or 0, respectively.
• There are 6 comparison operators that are common to see in order to
obtain a bool value:
• And there are three logical operators you need to be familiar with:
Strings
• Strings in Python are shown as the variable type str. You can define a
string with either double quotes " or single quotes '. If the string you
are creating actually has one of these two values in it, then you need
to be careful to assure your code doesn't give an error

• >>> my_string = 'this is a string!'


• >>> my_string2 = "this is also a string!!!"
• You can also include a \ in your string to be able to include one of
these quotes:
• >>> this_string = 'Simon\'s skateboard is in the garage.'
• >>> print(this_string)
• Simon's skateboard is in the garage.
• If we don't use this, notice we get the following error:
• >>> this_string = 'Simon's skateboard is in the garage.'
• File "<ipython-input-20-e80562c2a290>", line 1
• this_string = 'Simon's skateboard is in the garage.'
• ^
• SyntaxError: invalid syntax
• There are a number of other operations you can use with strings as
well. :
• >>> first_word = 'Hello'
• >>> second_word = 'There'
• >>> print(first_word + second_word)
• >>> first_word = 'Hello'
• >>> second_word = 'There'
• >>> print(first_word + second_word)

• HelloThere
• >>> first_word = 'Hello'
• >>> second_word = 'There'
• >>> print(first_word + second_word)
• HelloThere
• >>> print(first_word + ' ' + second_word)
• Hello There
• >>> print(first_word * 5)
• ??
• >>> print(len(first_word))
• ??
• >>> print(first_word * 5)
• HelloHelloHelloHelloHello
• >>> print(len(first_word))
•5
• Unlike the other data types you have seen so far, you can also index into strings,
• here is a small example. Notice Python uses 0 indexing –
• >>> first_word[0]

•H

• >>> first_word[1]

•e
• house_number = 13
• street_name = "The Crescent"
• town_home = "Belmont"
• print(type(house_name)) # <class 'int'="">

• address = str(house_number + " " + street_name + ", " + town_name


• print(address) # 13 The Crescent, Belmont

You might also like