Python How to Video Pt 2

Python PictureHere I continue my Python How to Program Tutorials. If you missed part 1, go watch Python How to Program Pt 1.

In this video tutorial, I’ll cover the integer and string variable types. I’ll also go over all of the different functions available to you for manipulating integers and strings in Python.

All of the code used, can be found after the video. If you have any questions or comments, leave them in the comment section below.

[adsense]

Here is All the Code Associated with the Integer’s Video

#!/usr/bin/python3

import math, decimal

# Demonstrate how to work with numbers in Python

def varEx():
_legalVarName = 1
Another1 = 2
yet_another = 3

print(”’and, continue, except, global, lambda, pass, while, as, def, + \
False, if, None, raise, with, assert, del, finally, import, nonlocal, + \
return, yield, break, elif, for, in, not, True, class, else, from, + \
is, or, try ”’) # \ is the line continuation character

x, y, z = 1, 2, 3

print(x, y, z)

def boolEx():
# Boolean Variables (True or False)
a = True
print(type(a))

a = 5
print(type(a))

print(bool(a))

b = True
c = False

print(b and c)

print(b or c)

print(not b)

d = (1 > 2)
print(” It is {} that 1 is greater than 2″.format(d))

def intEx():
# There are integers, binary, octal, hexidecimals

# print(d)

x = 4
y = 2

print(“x + y = “, x+y)
print(“x – y = “, x-y)
print(“x * y = “, x*y)
print(“x / y = “, x+y)
print(“x % y = “, 5%2)
print(“x // y = “, 5//2)
print(“x ** y = “, x**y)
print(“\n”)

print(“{} + {} = {}”.format(x, y, x+y))
print(“{} – {} = {}”.format(x, y, x-y))
print(“{} * {} = {}”.format(x, y, x*y))
print(“{} / {} = {}”.format(x, y, x/y))
print(“{} % {} = {}”.format(5, 2, 5%2))
print(“{} // {} = {}”.format(5, 2, 5//2))
print(“{} ** {} = {}”.format(x, y, x**y))

print(“Binary format for 4”, bin(x))
print(“divmod(5,2) = “,divmod(5,2))
print(“4 converted into a float”, float(x))
print(“Hexidecimal format for 4”, hex(x))
print(“4 raised to the power of 2”, pow(x,y))

z = 100 / 3
print(“100 divided by 3 = “, z)
print(“z is of type “, type(z))
print(“100 / 3 rounded to 3 decimals “, round(z, 3))

print(“math.ceil(100/3) = “, math.ceil(z))
print(“math.floor(100/3) = “, math.floor(z))

def floatEx(x=1, y=2):
z = x / y
print(z)
print(type(z))
print(id(z))

z = 1.1234567891011
print(z)

decimalVar = decimal.Decimal(“1.123456789101112131415”)
print(decimalVar)

decimalVar2 = decimal.Decimal(“2.345678910”)
print(decimalVar + decimalVar2)

varEx()
boolEx()
intEx()
floatEx(5, 2)

Here is All the Code Associated with the String Video

stringVar = “This is just a sample string”
print(stringVar)

numOne = 2
wordOne = “Cats”

print(“I have {} {}”.format(numOne, wordOne))

combineStr = “I have ” + str(numOne) + ” ” + wordOne
print(combineStr)

combineStr += ” I think?”
print(combineStr)

stringVar2 = “Just another random string”

for i in stringVar2:
print(i)

print(“\nThe first letter in the string is “, stringVar2[0])

for i in stringVar2:
print(i, end=”)

string3 = “””\nThis is a multiline
string that is great”””

print(string3)

print(“He said \”I love python\””)

string4 = “This is some really long text that I’m writing here”

print(string4)
print(string4.find(‘really’))
print(string4[13:19])

string4 = string4.replace(‘e’, ‘a’)

print(string4)

string5 = ‘     There is a bunch of white space here       ‘
print(string5)

string5 = string5.strip()
print(string5)

string5 = string5.split()
print(string5)

print(type(string5))

string5 = ‘ ‘.join(string5)
print(string5)

print(len(string5))

string5 = string5.center(20)
print(string5)

string6 = r”I don’t want to \n worry about backslashes”
print(string6)

Leave a Reply

Your email address will not be published.

Google+