INTERNSHIP AT PRU TECH
Day1 :16th may 2019
Thursday
Introduction to python
Basic Python
• 1.0 Variables
• 1.1 Manipulating Variables
• 1.2 Operators
Our first variable: Integers
In python an integer refers to a number that is does not have any decimal places.
Unlike other coding languages to initialize an integer, you just set a variable equal to
a number that does not have a decimal.
Example 1
#Declare the Variable (x can be equal to any integer)
x = 36
#Print out x with some text
print('x is equal to: ',x)
output:
x is equal to: 36
Example 2:
# Define the variable x
x=3
# Define the variable y
y=6
# Make z equal to the sum of x and y
z = x+y
# Print out some text and the value of variable z
print('The sum of the variables is: ',z)
output:
The sum of the variables is: 9
Numbers with Decimals
In python a float refers to a number that has decimal places.
# Define the variable
x = 3.2
# Print out the value
print('The value of x is:',x)
# Print out the type of variable x is
print(type(x))
output:
The value of x is: 3.2
<class 'float'>
Floats may also be in scientific notation, with E or e indicating the power of 10
(2.5e2 = 2.5 x 102 = 250).
If you add a float and an int together, it results in a float! An example of this is below,
and you can see the result is type float
Example 3:
# Declare x and y - One as an integer and one as a float
x = 3.2
y=3
# Create the sum
z = x+y
# Print out the value and the type
print(z)
print(type(z))
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 will pint the length of the string
print('The length of the string is: ', len(string1))
#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())
#This will print the string `HELLO WORLD`
print('The string in all UPPER case letters is: ', string1.upper())
#This will print the two strings 'Hello', 'World'
print('The string split into a bunch of words is: ',string1.split(' '))
OUTPUT:
The length of the string is: 11
The string in all lower case letters is: hello world
The string in all UPPER case letters is: HELLO WORLD
The string split into a bunch of words is: ['Hello', 'World']
Algebra
Operations in python
• * = Multiplication
• Can be used with floats and integers
• + = Addition
• Can be used with floats, integers and strings
• - = Subtraction
• Can be used with floats and integers
• / = Division
• Can be used with floats and integers
Other functions:
• ** = Exponential
• // Integer Division
• Integer division is division in which the fractional part (remainder) is discarded is
called integer division.
• % Modulus 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
# Add x and y and print the result
print('The sum of x and y is: ',x + y)
# Subtract x and y and print the result
print('The difference of x and y is: ', x - y)
# Multiply x and y and print the result
print('x * y equals: ', x * y)
# Divide x and y and print the result
print('x / y = ', x / y)
output:
The sum of x and y is: 5
The difference of x and y is: -1
x * y equals: 6
x / y = 0.6666666666666666
Lists and Collections
First up: Collections
Storing Data in a python list (One of the most useful data structures that you will find are lists.)
Example 7:
# list with Numbers
lst1 = [1,2,3,4]
# list with strings
lst2 = ['strings', 'can', 'go', 'in', 'here','too']
# list with strings and numbers
lst3 = [1,2,'string','another string']
# Print each out:
print(lst1)
print(lst2)
print(lst3)
output:
[1, 2, 3, 4]
['strings', 'can', 'go', 'in', 'here', 'too']
[1, 2, 'string', 'another string']
in python index always start at 0 and you can access any element of a list using its
index.
Manipulating Varables
Functions and Iterables
Creating for loops and putting it into a function
• Since we just introduced a list, one of the most useful parts of a list is that they are an
iterable
An iterable is a collection of data that you can move through using a for loop
For Loop Syntax in python
Example 8:
# For loop through a list of Numbers
lst = [1,2,3,4]
# "Iterate" through each number in the list
for number in lst:
# Print the number during each iteration
print(number)
output:
1
2
3
4
Example 9:
# For loop through each letter in the string
string = 'Hello'
# Iterate through each letter in the string
for letter in string:
# Print each letter during the iterations
print(letter)
OUTPUT:
H
e
l
l
o
Printing words in a list
# Declare the list of words
lst_string = ['Hello','World']
# Iterate through each word in the list
for word in lst_string:
# Print the word during each iteration
print(word)
output:
Hello
World
The range() Function
To loop through a set of code a specified number of times we can use the range() function.
# Create an iterable using range
for x in range(6):
# print the value of the iterable during each loop
print(x)
output:
0
1
2
3
4
5
Operations on variables and values
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
The in operator
Membership operators are used to test if a sequence is presented in an object: