0% found this document useful (0 votes)
22 views

Data Types, User Input and Control Flow Python Codes 13th Nov 2024

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Data Types, User Input and Control Flow Python Codes 13th Nov 2024

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Visual Studio Code Shortcuts

Data Types
New File -> data_types.py Ctrl+s (Save)

# String data type

# 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

Open New Terminal

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.)

fullname = first + " " + last

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 = '''

Hey, how are you?

I was just checking in.

All good?

'''

print(multiline)

# Escaping special characters

sentence = 'I\'m back at work!\tHey!\n\nWhere\'s this at\\located?'

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))

multiline += " "

multiline = " " + 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("Coffee".ljust(16, ".") + "$1".rjust(4))

print("Muffin".ljust(16, ".") + "$2".rjust(4))

print("Cheesecake".ljust(16, ".") + "$4".rjust(4))

print("")

# string index values

print(first[1])

print(first[-1])

print(first[1:-1])

print(first[1:])

# Some methods return boolean data

print(first.startswith("D"))

print(first.endswith("Z"))

# Boolean data type

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)

# Built-in functions for numbers

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))

# Casting a string to a number

zipcode = "10001"

zip_value = int(zipcode)

print(type(zip_value))

# Error if you attempt to cast incorrect data

# zip_value = int("New York")


How to create a game using User Input and Control Flow in Python

import sys

import random

from enum import Enum

class RPS(Enum):

ROCK = 1

PAPER = 2

SCISSORS = 3

print("")

playerchoice = input(

"Enter...\n1 for Rock,\n2 for Paper, or \n3 for Scissors:\n\n")

player = int(playerchoice)

if player < 1 or player > 3:

sys.exit("You must enter 1, 2, or 3.")

computerchoice = random.choice("123")

computer = int(computerchoice)

print("")

print("You chose " + str(RPS(player)).replace('RPS.', '') + ".")

print("Python chose " + str(RPS(computer)).replace('RPS.', '') + ".")


print("")

if player == 1 and computer == 3:

print(" You win!")

elif player == 2 and computer == 1:

print(" You win!")

elif player == 3 and computer == 2:

print(" You win!")

elif player == computer:

print(" Tie game!")

else:

print(" Python wins!")

You might also like