Data Types, User Input and Control Flow Python Codes 13th Nov 2024
Data Types, User Input and Control Flow Python Codes 13th Nov 2024
Data Types
New File -> data_types.py Ctrl+s (Save)
# literal assignment (Literals in python are fixed values that represent constant data in Python
code. They are utilized to relegate values to variables, define constants, and perform different
operations within the code.)
first = "Edwin"
last = "TS"
# print(type(first))
# print(type(first) == str)
# print(isinstance(first, str)) Note: isinstance is a function
Output:
<class ‘str’>
True
True
# constructor function (Constructors in Python is a special class method for creating and initializing
an object instance at that class.)
# pizza = str("Pepperoni")
# print(type(pizza))
# print(type(pizza) == str)
# print(isinstance(pizza, str))
# Concatenation (String concatenation means add strings together. Use the + character to add a
variable to another variable.)
print(fullname)
fullname += "!"
print(fullname)
# Casting a number to a string (The str() function is the simplest and most commonly used method
to convert an integer to a string. Python.)
decade = str(1980)
print(type(decade))
print(decade)
statement = "I like rock music from the " + decade + "s."
print(statement)
# Multiple lines
multiline = '''
All good?
'''
print(multiline)
print(sentence)
# String Methods
print(first)
print(first.lower())
print(first.upper())
print(first)
print(multiline.title())
print(multiline.replace("good", "ok"))
print(multiline)
print(len(multiline))
print(len(multiline))
print(len(multiline.strip()))
print(len(multiline.lstrip()))
print(len(multiline.rstrip()))
print("")
# Build a menu
title = "menu".upper()
print(title.center(20, "="))
print("")
print(first[1])
print(first[-1])
print(first[1:-1])
print(first[1:])
print(first.startswith("D"))
print(first.endswith("Z"))
myvalue = True
x = bool(False)
print(type(x))
print(isinstance(myvalue, bool))
# Numeric data types
# integer type
price = 100
best_price = int(80)
print(type(price))
print(isinstance(best_price, int))
# float type
gpa = 3.28
y = float(1.14)
print(type(gpa))
# complex type
comp_value = 5+3j
print(type(comp_value))
print(comp_value.real)
print(comp_value.imag)
print(abs(gpa))
print(abs(gpa * -1))
print(round(gpa))
print(round(gpa, 1))
print(math.pi)
print(math.sqrt(64))
print(math.ceil(gpa))
print(math.floor(gpa))
zipcode = "10001"
zip_value = int(zipcode)
print(type(zip_value))
import sys
import random
class RPS(Enum):
ROCK = 1
PAPER = 2
SCISSORS = 3
print("")
playerchoice = input(
player = int(playerchoice)
computerchoice = random.choice("123")
computer = int(computerchoice)
print("")
else: