├── AddStringInt.py ├── ComparisonOperators.py ├── Elif.py ├── Else.py ├── EvenOdd.py ├── HelloWorld.py ├── IntToFloat.py ├── Logical.py ├── LogicalOperator.py ├── PythonArray.py ├── PythonClass.py ├── PythonClasses.py ├── PythonDeleteFile.py ├── PythonDeleteFolder.py ├── PythonDictionary.py ├── PythonExample.py ├── PythonExample1.py ├── PythonFileRead.py ├── PythonFileWrite.py ├── PythonFunction.py ├── PythonIf.py ├── PythonInheritance.py ├── PythonInputFormatting.py ├── PythonIterator.py ├── PythonJSON.py ├── PythonList.py ├── PythonLoop.py ├── PythonMathFunction.py ├── PythonNumbers.py ├── PythonObject.py ├── PythonReadFile.py ├── PythonRegEx.py ├── PythonSet.py ├── PythonString.py ├── PythonStrings.py ├── PythonTuple.py ├── PythonUserInput.py ├── PytonOutputFormatting.py ├── README.md ├── RecursiveFactorial.py ├── StringToInt.py ├── arithmeticoperator.py ├── basic.py ├── basics.py ├── bool.py ├── boolarray.py ├── boolean.py ├── booll.py ├── boolvalue.py ├── boolvalues.py ├── capitalize.py ├── case.py ├── casee.py ├── casefold.py ├── center.py ├── checkStr.py ├── checkstring.py ├── comment.py ├── commentss.py ├── comple.py ├── coms.py ├── conversion.py ├── count.py ├── datatype.py ├── datatypes,py ├── date.py ├── date1.py ├── date2.py ├── dict.py ├── encode.py ├── end.py ├── escape.py ├── escapeee.py ├── escapesequence.py ├── factorial.py ├── factorial1.py ├── file.py ├── find.py ├── float.py ├── formatstring.py ├── funct.py ├── function.py ├── functionn.py ├── globvariable.py ├── glvariable.py ├── hello.py ├── identifyoperator.py ├── index.py ├── indexx.py ├── ini.py ├── instance.py ├── intro.py ├── introu.py ├── largestnumber.py ├── leapyear.py ├── loop.py ├── lower.py ├── membershipoperator.py ├── multiline.py ├── multilinee.py ├── multiplevariables.py ├── multiplicationtable.py ├── multivariable.py ├── number.py ├── number1.py ├── numbers.py ├── onevalue.py ├── operators.py ├── operators1.py ├── outvariable.py ├── outvariables.py ├── price.py ├── prime.py ├── prime1.py ├── primenumber.py ├── quotes.py ├── random.py ├── remwhitspace.py ├── rpps.py ├── rpps_exit.py ├── rpps_lz.py ├── rpps_rigged.py ├── singledouble.py ├── slic.py ├── slice.py ├── slicee.py ├── stringconcat.py ├── stringformat.py ├── stringrepl.py ├── strings.py ├── stringsplit.py ├── stringss.py ├── stringvariable.py ├── strlen.py ├── syntax.py ├── syntaxx.py ├── test.py ├── true.py ├── type.py ├── typeOf.py ├── typeof.py ├── unpack.py ├── uppercase.py ├── var.py ├── variaable.py ├── variable.py ├── variables.py ├── vars.py └── whitespace.py /AddStringInt.py: -------------------------------------------------------------------------------- 1 | num_int = 123 2 | num_str = "456" 3 | 4 | print("Data type of num_int:",type(num_int)) 5 | print("Data type of num_str before Type Casting:",type(num_str)) 6 | 7 | num_str = int(num_str) 8 | print("Data type of num_str after Type Casting:",type(num_str)) 9 | 10 | num_sum = num_int + num_str 11 | 12 | print("Sum of num_int and num_str:",num_sum) 13 | print("Data type of the sum:",type(num_sum)) 14 | -------------------------------------------------------------------------------- /ComparisonOperators.py: -------------------------------------------------------------------------------- 1 | x = 10 2 | y = 12 3 | 4 | # Output: x > y is False 5 | print('x > y is',x>y) 6 | 7 | # Output: x < y is True 8 | print('x < y is',x= y is False 17 | print('x >= y is',x>=y) 18 | 19 | # Output: x <= y is True 20 | print('x <= y is',x<=y) 21 | -------------------------------------------------------------------------------- /Elif.py: -------------------------------------------------------------------------------- 1 | #ELIF Example 2 | a = 33 3 | b = 33 4 | if b > a: 5 | print("b is greater than a") 6 | elif a == b: 7 | print("a and b are equal") 8 | -------------------------------------------------------------------------------- /Else.py: -------------------------------------------------------------------------------- 1 | a = 200 2 | b = 33 3 | if b > a: 4 | print("b is greater than a") 5 | elif a == b: 6 | print("a and b are equal") 7 | else: 8 | print("a is greater than b") 9 | -------------------------------------------------------------------------------- /EvenOdd.py: -------------------------------------------------------------------------------- 1 | # Python program to check if the input number is odd or even. 2 | # A number is even if division by 2 gives a remainder of 0. 3 | # If the remainder is 1, it is an odd number. 4 | 5 | num = int(input("Enter a number: ")) 6 | if (num % 2) == 0: 7 | print("{0} is Even".format(num)) 8 | else: 9 | print("{0} is Odd".format(num)) 10 | -------------------------------------------------------------------------------- /HelloWorld.py: -------------------------------------------------------------------------------- 1 | print("Hello, World!") 2 | -------------------------------------------------------------------------------- /IntToFloat.py: -------------------------------------------------------------------------------- 1 | num_int = 123 2 | num_flo = 1.23 3 | 4 | num_new = num_int + num_flo 5 | 6 | print("datatype of num_int:",type(num_int)) 7 | print("datatype of num_flo:",type(num_flo)) 8 | 9 | print("Value of num_new:",num_new) 10 | print("datatype of num_new:",type(num_new)) 11 | 12 | -------------------------------------------------------------------------------- /Logical.py: -------------------------------------------------------------------------------- 1 | x = True 2 | y = False 3 | 4 | print('x and y is',x and y) 5 | 6 | print('x or y is',x or y) 7 | 8 | print('not x is',not x) 9 | -------------------------------------------------------------------------------- /LogicalOperator.py: -------------------------------------------------------------------------------- 1 | x = True 2 | y = False 3 | 4 | print('x and y is',x and y) 5 | 6 | print('x or y is',x or y) 7 | 8 | print('not x is',not x) 9 | -------------------------------------------------------------------------------- /PythonArray.py: -------------------------------------------------------------------------------- 1 | #Example array 2 | cars = ["Ford", "Volvo", "BMW"] 3 | 4 | x = cars[0] 5 | 6 | print(x) 7 | 8 | #Example length of array 9 | cars = ["Ford", "Volvo", "BMW"] 10 | 11 | x = len(cars) 12 | 13 | print(x) 14 | -------------------------------------------------------------------------------- /PythonClass.py: -------------------------------------------------------------------------------- 1 | class MyClass: 2 | x = 5 3 | 4 | print(MyClass) 5 | -------------------------------------------------------------------------------- /PythonClasses.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | def __init__(self, name, age): 3 | self.name = name 4 | self.age = age 5 | 6 | p1 = Person("John", 36) 7 | 8 | print(p1.name) 9 | print(p1.age) 10 | -------------------------------------------------------------------------------- /PythonDeleteFile.py: -------------------------------------------------------------------------------- 1 | import os 2 | if os.path.exists("demofile.txt"): 3 | os.remove("demofile.txt") 4 | else: 5 | print("The file does not exist") 6 | -------------------------------------------------------------------------------- /PythonDeleteFolder.py: -------------------------------------------------------------------------------- 1 | import os 2 | os.rmdir("myfolder") 3 | -------------------------------------------------------------------------------- /PythonDictionary.py: -------------------------------------------------------------------------------- 1 | d = {1:'value','key':2} 2 | print(type(d)) 3 | 4 | print("d[1] = ", d[1]) 5 | 6 | print("d['key'] = ", d['key']) 7 | 8 | # Generates error 9 | print("d[2] = ", d[2]) 10 | -------------------------------------------------------------------------------- /PythonExample.py: -------------------------------------------------------------------------------- 1 | #Example 2 | if 5 > 2: 3 | print("Five is greater than two!") 4 | 5 | #Example 6 | if 5 > 2: 7 | print("Five is greater than two!") 8 | if 5 > 2: 9 | print("Five is greater than two!") 10 | 11 | #Example 12 | x = 5 13 | y = "Hello, World!" 14 | print(x) 15 | print(y) 16 | 17 | #Example 18 | #This is a comment. 19 | print("Hello, World!") 20 | 21 | #Example 22 | print("Hello, World!") #This is a comment 23 | 24 | #Example 25 | #print("Hello, World!") 26 | print("Cheers, Mate!") 27 | 28 | #Example 29 | #This is a comment 30 | #written in 31 | #more than just one line 32 | print("Hello, World!") 33 | 34 | #Example 35 | """ 36 | This is a comment 37 | written in 38 | more than just one line 39 | """ 40 | print("Hello, World!") 41 | 42 | -------------------------------------------------------------------------------- /PythonExample1.py: -------------------------------------------------------------------------------- 1 | a = 200 2 | b = 33 3 | if b > a: 4 | print("b is greater than a") 5 | else: 6 | print("b is not greater than a") 7 | 8 | #Example 9 | if a > b: print("a is greater than b") 10 | 11 | #Example 12 | a = 2 13 | b = 330 14 | print("A") if a > b else print("B") 15 | 16 | #Example 17 | a = 330 18 | b = 330 19 | print("A") if a > b else print("=") if a == b else print("B") 20 | 21 | #AND Keyword 22 | a = 200 23 | b = 33 24 | c = 500 25 | if a > b and c > a: 26 | print("Both conditions are True") 27 | -------------------------------------------------------------------------------- /PythonFileRead.py: -------------------------------------------------------------------------------- 1 | f = open("demofile.txt", "r") 2 | print(f.read(5)) 3 | -------------------------------------------------------------------------------- /PythonFileWrite.py: -------------------------------------------------------------------------------- 1 | f = open("demofile2.txt", "a") 2 | f.write("Now the file has more content!") 3 | f.close() 4 | 5 | #open and read the file after the appending: 6 | f = open("demofile2.txt", "r") 7 | print(f.read()) 8 | -------------------------------------------------------------------------------- /PythonFunction.py: -------------------------------------------------------------------------------- 1 | #Example 2 | def my_function(): 3 | print("Hello from a function") 4 | 5 | #Example 6 | def my_function(): 7 | print("Hello from a function") 8 | 9 | my_function() 10 | 11 | #Example 12 | def my_function(fname): 13 | print(fname + " Refsnes") 14 | 15 | my_function("Emil") 16 | my_function("Tobias") 17 | my_function("Linus") 18 | -------------------------------------------------------------------------------- /PythonIf.py: -------------------------------------------------------------------------------- 1 | #IF Example 2 | a = 33 3 | b = 200 4 | if b > a: 5 | print("b is greater than a") 6 | 7 | -------------------------------------------------------------------------------- /PythonInheritance.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | def __init__(self, fname, lname): 3 | self.firstname = fname 4 | self.lastname = lname 5 | 6 | def printname(self): 7 | print(self.firstname, self.lastname) 8 | 9 | #Use the Person class to create an object, and then execute the printname method: 10 | 11 | x = Person("John", "Doe") 12 | x.printname() 13 | -------------------------------------------------------------------------------- /PythonInputFormatting.py: -------------------------------------------------------------------------------- 1 | import math 2 | print(math.pi) 3 | -------------------------------------------------------------------------------- /PythonIterator.py: -------------------------------------------------------------------------------- 1 | mytuple = ("apple", "banana", "cherry") 2 | myit = iter(mytuple) 3 | 4 | print(next(myit)) 5 | print(next(myit)) 6 | print(next(myit)) 7 | -------------------------------------------------------------------------------- /PythonJSON.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | # some JSON: 4 | x = '{ "name":"John", "age":30, "city":"New York"}' 5 | 6 | # parse x: 7 | y = json.loads(x) 8 | 9 | # the result is a Python dictionary: 10 | print(y["age"]) 11 | -------------------------------------------------------------------------------- /PythonList.py: -------------------------------------------------------------------------------- 1 | a = [5,10,15,20,25,30,35,40] 2 | 3 | # a[2] = 15 4 | print("a[2] = ", a[2]) 5 | 6 | # a[0:3] = [5, 10, 15] 7 | print("a[0:3] = ", a[0:3]) 8 | 9 | # a[5:] = [30, 35, 40] 10 | print("a[5:] = ", a[5:]) 11 | 12 | #immutable list 13 | a = [1, 2, 3] 14 | a[2] = 4 15 | print(a) 16 | -------------------------------------------------------------------------------- /PythonLoop.py: -------------------------------------------------------------------------------- 1 | #Looping Array 2 | cars = ["Ford", "Volvo", "BMW"] 3 | 4 | for x in cars: 5 | print(x) 6 | -------------------------------------------------------------------------------- /PythonMathFunction.py: -------------------------------------------------------------------------------- 1 | x = min(5, 10, 25) 2 | y = max(5, 10, 25) 3 | 4 | print(x) 5 | print(y) 6 | -------------------------------------------------------------------------------- /PythonNumbers.py: -------------------------------------------------------------------------------- 1 | a = 5 2 | print(a, "is of type", type(a)) 3 | 4 | a = 2.0 5 | print(a, "is of type", type(a)) 6 | 7 | a = 1+2j 8 | print(a, "is complex number?", isinstance(1+2j,complex)) 9 | -------------------------------------------------------------------------------- /PythonObject.py: -------------------------------------------------------------------------------- 1 | class MyClass: 2 | x = 5 3 | 4 | p1 = MyClass() 5 | print(p1.x) 6 | -------------------------------------------------------------------------------- /PythonReadFile.py: -------------------------------------------------------------------------------- 1 | f = open("demofile.txt", "r") 2 | 3 | print(f.read()) 4 | -------------------------------------------------------------------------------- /PythonRegEx.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | #Check if the string starts with "The" and ends with "Spain": 4 | 5 | txt = "The rain in Spain" 6 | x = re.search("^The.*Spain$", txt) 7 | 8 | if x: 9 | print("YES! We have a match!") 10 | else: 11 | print("No match") 12 | -------------------------------------------------------------------------------- /PythonSet.py: -------------------------------------------------------------------------------- 1 | a = {5,2,3,1,4} 2 | 3 | # printing set variable 4 | print("a = ", a) 5 | 6 | # data type of variable a 7 | print(type(a)) 8 | 9 | #Example 10 | 11 | a = {1,2,2,3,3,3} 12 | print(a) 13 | -------------------------------------------------------------------------------- /PythonString.py: -------------------------------------------------------------------------------- 1 | price = 49 2 | txt = "The price is {} dollars" 3 | print(txt.format(price)) 4 | -------------------------------------------------------------------------------- /PythonStrings.py: -------------------------------------------------------------------------------- 1 | s = "This is a string" 2 | print(s) 3 | s = '''A multiline 4 | string''' 5 | print(s) 6 | -------------------------------------------------------------------------------- /PythonTuple.py: -------------------------------------------------------------------------------- 1 | t = (5,'program', 1+3j) 2 | 3 | # t[1] = 'program' 4 | print("t[1] = ", t[1]) 5 | 6 | # t[0:3] = (5, 'program', (1+3j)) 7 | print("t[0:3] = ", t[0:3]) 8 | 9 | # Generates error 10 | # Tuples are immutable 11 | t[0] = 10 12 | -------------------------------------------------------------------------------- /PythonUserInput.py: -------------------------------------------------------------------------------- 1 | username = input("Enter username:") 2 | print("Username is: " + username) 3 | -------------------------------------------------------------------------------- /PytonOutputFormatting.py: -------------------------------------------------------------------------------- 1 | print('This sentence is output to the screen') 2 | 3 | a = 5 4 | print('The value of a is', a) 5 | 6 | print(1, 2, 3, 4) 7 | print(1, 2, 3, 4, sep='*') 8 | print(1, 2, 3, 4, sep='#', end='&') 9 | 10 | print('I love {0} and {1}'.format('bread','butter')) 11 | print('I love {1} and {0}'.format('bread','butter')) 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python 2 | Python Basic Programs 3 | -------------------------------------------------------------------------------- /RecursiveFactorial.py: -------------------------------------------------------------------------------- 1 | def factorial(x): 2 | """This is a recursive function 3 | to find the factorial of an integer""" 4 | 5 | if x == 1: 6 | return 1 7 | else: 8 | return (x * factorial(x-1)) 9 | 10 | 11 | num = 3 12 | print("The factorial of", num, "is", factorial(num)) 13 | -------------------------------------------------------------------------------- /StringToInt.py: -------------------------------------------------------------------------------- 1 | num_int = 123 2 | num_str = "456" 3 | 4 | print("Data type of num_int:",type(num_int)) 5 | print("Data type of num_str:",type(num_str)) 6 | 7 | print(num_int+num_str) 8 | -------------------------------------------------------------------------------- /arithmeticoperator.py: -------------------------------------------------------------------------------- 1 | x = 15 2 | y = 4 3 | 4 | # Output: x + y = 19 5 | print('x + y =',x+y) 6 | 7 | # Output: x - y = 11 8 | print('x - y =',x-y) 9 | 10 | # Output: x * y = 60 11 | print('x * y =',x*y) 12 | 13 | # Output: x / y = 3.75 14 | print('x / y =',x/y) 15 | 16 | # Output: x // y = 3 17 | print('x // y =',x//y) 18 | 19 | # Output: x ** y = 50625 20 | print('x ** y =',x**y) 21 | -------------------------------------------------------------------------------- /basic.py: -------------------------------------------------------------------------------- 1 | x = 18 2 | y = "Hello, World!" 3 | z = 2 4 | 5 | print(z+x) 6 | -------------------------------------------------------------------------------- /basics.py: -------------------------------------------------------------------------------- 1 | if 10 > 2: 2 | print("Ten is greater than two!") 3 | 4 | if 15 > 9: 5 | print("15 is greater than nine!") 6 | -------------------------------------------------------------------------------- /bool.py: -------------------------------------------------------------------------------- 1 | print(bool("Hi")) 2 | print(bool(25)) 3 | -------------------------------------------------------------------------------- /boolarray.py: -------------------------------------------------------------------------------- 1 | bool("abc") 2 | bool(123) 3 | bool(["apple", "cherry", "banana"]) 4 | -------------------------------------------------------------------------------- /boolean.py: -------------------------------------------------------------------------------- 1 | print(10 > 9) 2 | print(10 == 9) 3 | print(10 < 9) 4 | -------------------------------------------------------------------------------- /booll.py: -------------------------------------------------------------------------------- 1 | x = "Hi" 2 | y = 25 3 | 4 | print(bool(x)) 5 | print(bool(y)) 6 | -------------------------------------------------------------------------------- /boolvalue.py: -------------------------------------------------------------------------------- 1 | class myclass(): 2 | def __len__(self): 3 | return 0 4 | 5 | myobj = myclass() 6 | print(bool(myobj)) 7 | -------------------------------------------------------------------------------- /boolvalues.py: -------------------------------------------------------------------------------- 1 | print(bool(False)) 2 | print(bool(None)) 3 | print(bool(0)) 4 | print(bool("")) 5 | print(bool(())) 6 | print(bool([])) 7 | print(bool({})) 8 | -------------------------------------------------------------------------------- /capitalize.py: -------------------------------------------------------------------------------- 1 | Capitalize = "hello, and welcome to my world." 2 | 3 | x = Capitalize.capitalize() 4 | 5 | print (x) 6 | -------------------------------------------------------------------------------- /case.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | a = "4" 4 | A = "Sally" 5 | 6 | print(a +" "+ A) -------------------------------------------------------------------------------- /casee.py: -------------------------------------------------------------------------------- 1 | a = "10" 2 | A = "Joe" 3 | 4 | print(a +" "+ A) 5 | -------------------------------------------------------------------------------- /casefold.py: -------------------------------------------------------------------------------- 1 | Casefold = "Hello, And Welcome To My World!" 2 | 3 | x = Casefold.casefold() 4 | 5 | print(x) 6 | -------------------------------------------------------------------------------- /center.py: -------------------------------------------------------------------------------- 1 | Center = "apples" 2 | 3 | x = Center.center(20) 4 | 5 | print(x) 6 | -------------------------------------------------------------------------------- /checkStr.py: -------------------------------------------------------------------------------- 1 | txt = "Python is easy and simple!" 2 | print("easy" in txt) 3 | 4 | txt = "Hello World" 5 | if "World" in txt: 6 | print("Yes, 'World' is present.") 7 | -------------------------------------------------------------------------------- /checkstring.py: -------------------------------------------------------------------------------- 1 | txt = "Hello World!" 2 | print("Hi" not in txt) 3 | 4 | txt = "Python is easy and simple" 5 | if "understand" not in txt: 6 | print("Yes, 'understand' is NOT present.") 7 | -------------------------------------------------------------------------------- /comment.py: -------------------------------------------------------------------------------- 1 | #This is a comment. 2 | print("Hello, World!") 3 | 4 | ''' This is also a Comment ''' 5 | 6 | print("Comment") #This is a comment 7 | 8 | #This is a comment 9 | #written in 10 | #more than just one line 11 | print("Comment!") 12 | 13 | """ 14 | This is a comment 15 | written in 16 | more than just one line 17 | """ 18 | print("Comment''' ") -------------------------------------------------------------------------------- /commentss.py: -------------------------------------------------------------------------------- 1 | #Comment in Python. 2 | print("Hello, World!") 3 | 4 | ''' Comment in Python ''' 5 | 6 | print("Comment") #Comment in Python 7 | 8 | #Comment in Python 9 | #with 10 | #more than one line 11 | print("Comment!") 12 | 13 | """ 14 | Comment 15 | written in 16 | more than one line 17 | """ 18 | print("Comment''' ") 19 | -------------------------------------------------------------------------------- /comple.py: -------------------------------------------------------------------------------- 1 | x = 3+5j 2 | y = 5j 3 | z = -5j 4 | 5 | print(type(x)) 6 | print(type(y)) 7 | print(type(z)) 8 | -------------------------------------------------------------------------------- /coms.py: -------------------------------------------------------------------------------- 1 | #This is a comment. 2 | print("Hello, Python!") 3 | -------------------------------------------------------------------------------- /conversion.py: -------------------------------------------------------------------------------- 1 | x = 1 2 | y = 2.8 3 | z = 1j 4 | 5 | 6 | a = float(x) 7 | 8 | 9 | b = int(y) 10 | 11 | 12 | c = complex(x) 13 | 14 | print(a) 15 | print(b) 16 | print(c) 17 | 18 | print(type(a)) 19 | print(type(b)) 20 | print(type(c)) 21 | -------------------------------------------------------------------------------- /count.py: -------------------------------------------------------------------------------- 1 | Count = "Hello,Hello,Hello,Hello,Hello,Hello,Hello,Hello,Hello,Hello,Hello,Hello,Hello,Hello,Hello." 2 | 3 | x = Count.count("Hello") 4 | 5 | print(x) 6 | -------------------------------------------------------------------------------- /datatype.py: -------------------------------------------------------------------------------- 1 | x = 10 2 | print(type(x)) 3 | -------------------------------------------------------------------------------- /datatypes,py: -------------------------------------------------------------------------------- 1 | a = "Hi" #str 2 | b = 10 #int 3 | c = 10.5 #float 4 | d = 2j #complex 5 | e = ["orange", "banana", "cherry"] #list 6 | f = ("orange", "banana", "cherry") #tuple 7 | g = range(6) #range 8 | h = {"name" : "Joe", "age" : 56} #dict 9 | i = {"orange", "banana", "cherry"} #set 10 | j = frozenset({"orange", "banana", "cherry"}) #frozenset 11 | k = True #bool 12 | l = b"Hi" #bytes 13 | m = bytearray(5) #bytearray 14 | n = memoryview(bytes(5)) #memoryview 15 | 16 | print(type(a)) 17 | print(type(b)) 18 | print(type(c)) 19 | print(type(d)) 20 | print(type(e)) 21 | print(type(f)) 22 | print(type(g)) 23 | print(type(h)) 24 | print(type(i)) 25 | print(type(j)) 26 | print(type(k)) 27 | print(type(l)) 28 | print(type(m)) 29 | print(type(n)) 30 | 31 | -------------------------------------------------------------------------------- /date.py: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | 3 | today = date.today() 4 | print("Today's date:", today) 5 | -------------------------------------------------------------------------------- /date1.py: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | 3 | today = date.today() 4 | 5 | # dd/mm/YY 6 | d1 = today.strftime("%d/%m/%Y") 7 | print("d1 =", d1) 8 | 9 | # Textual month, day and year 10 | d2 = today.strftime("%B %d, %Y") 11 | print("d2 =", d2) 12 | 13 | # mm/dd/y 14 | d3 = today.strftime("%m/%d/%y") 15 | print("d3 =", d3) 16 | 17 | # Month abbreviation, day and year 18 | d4 = today.strftime("%b-%d-%Y") 19 | print("d4 =", d4) 20 | -------------------------------------------------------------------------------- /date2.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | 3 | # datetime object containing current date and time 4 | now = datetime.now() 5 | 6 | print("now =", now) 7 | 8 | # dd/mm/YY H:M:S 9 | dt_string = now.strftime("%d/%m/%Y %H:%M:%S") 10 | print("date and time =", dt_string) 11 | -------------------------------------------------------------------------------- /dict.py: -------------------------------------------------------------------------------- 1 | mylist = ["a", "b", "a", "c", "c"] 2 | mylist = list(dict.fromkeys(mylist)) 3 | print(mylist) 4 | 5 | -------------------------------------------------------------------------------- /encode.py: -------------------------------------------------------------------------------- 1 | Encode = "My name is Valentine" 2 | 3 | x = Encode.encode() 4 | 5 | print(x) 6 | -------------------------------------------------------------------------------- /end.py: -------------------------------------------------------------------------------- 1 | EndsWith = "Hello, welcome to my world!" 2 | 3 | x = EndsWith.endswith(".") 4 | 5 | print(x) 6 | -------------------------------------------------------------------------------- /escape.py: -------------------------------------------------------------------------------- 1 | txt = "We are the so-called \"Vikings\" from the north." 2 | print(txt) 3 | -------------------------------------------------------------------------------- /escapeee.py: -------------------------------------------------------------------------------- 1 | EraseBackSpace = "Little \bSpace!" 2 | print(EraseBackSpace) 3 | 4 | txt = "\110\145\154\154\157" 5 | print(txt) 6 | 7 | txt = "\x48\x65\x6c\x6c\x6f" 8 | print(txt) 9 | -------------------------------------------------------------------------------- /escapesequence.py: -------------------------------------------------------------------------------- 1 | SingleQuote = "The humans are \'Inhumans\' sometimes." 2 | print(SingleQuote) 3 | 4 | Backslash = "This will insert one \\ (backslash)." 5 | print(Backslash) 6 | 7 | NewLine = "Hello\nWorld!" 8 | print(NewLine) 9 | 10 | CarriageReturn = "Hello\rWorld!" 11 | print(CarriageReturn) 12 | 13 | AnSpace = "So\tSpace!" 14 | print(AnSpace) 15 | -------------------------------------------------------------------------------- /factorial.py: -------------------------------------------------------------------------------- 1 | # Python program to find the factorial of a number provided by the user. 2 | 3 | # change the value for a different result 4 | num = 7 5 | 6 | # To take input from the user 7 | #num = int(input("Enter a number: ")) 8 | 9 | factorial = 1 10 | 11 | # check if the number is negative, positive or zero 12 | if num < 0: 13 | print("Sorry, factorial does not exist for negative numbers") 14 | elif num == 0: 15 | print("The factorial of 0 is 1") 16 | else: 17 | for i in range(1,num + 1): 18 | factorial = factorial*i 19 | print("The factorial of",num,"is",factorial) 20 | -------------------------------------------------------------------------------- /factorial1.py: -------------------------------------------------------------------------------- 1 | # Python program to find the factorial of a number provided by the user 2 | # using recursion 3 | 4 | def factorial(x): 5 | """This is a recursive function 6 | to find the factorial of an integer""" 7 | 8 | if x == 1: 9 | return 1 10 | else: 11 | # recursive call to the function 12 | return (x * factorial(x-1)) 13 | 14 | 15 | # change the value for a different result 16 | num = 7 17 | 18 | # to take input from the user 19 | # num = int(input("Enter a number: ")) 20 | 21 | # call the factorial function 22 | result = factorial(num) 23 | print("The factorial of", num, "is", result) 24 | -------------------------------------------------------------------------------- /file.py: -------------------------------------------------------------------------------- 1 | f = open("demofile.txt") 2 | -------------------------------------------------------------------------------- /find.py: -------------------------------------------------------------------------------- 1 | Finder = "Hello, welcome to my world." 2 | 3 | x = Finder.find("welcome") 4 | 5 | print(x) 6 | -------------------------------------------------------------------------------- /float.py: -------------------------------------------------------------------------------- 1 | x = 1.10 2 | y = 1.0 3 | z = -35.59 4 | 5 | print(type(x)) 6 | print(type(y)) 7 | print(type(z)) 8 | 9 | a = 35e3 10 | b = 12E4 11 | c = -87.7e100 12 | 13 | print(type(a)) 14 | print(type(b)) 15 | print(type(c)) 16 | -------------------------------------------------------------------------------- /formatstring.py: -------------------------------------------------------------------------------- 1 | quantity = 2 2 | items = 500 3 | price = 50.50 4 | myorder = "I want {} pieces of item {} for {} dollars." 5 | print(myorder.format(quantity, items, price)) 6 | 7 | quantity = 5 8 | items = 600 9 | price = 100.50 10 | myorder = "I want to pay {2} dollars for {0} pieces of item {1}." 11 | print(myorder.format(quantity, items, price)) 12 | -------------------------------------------------------------------------------- /funct.py: -------------------------------------------------------------------------------- 1 | def myFunction() : 2 | return True 3 | 4 | print(myFunction()) 5 | -------------------------------------------------------------------------------- /function.py: -------------------------------------------------------------------------------- 1 | def greet(name): 2 | """ 3 | This function greets to 4 | the person passed in as 5 | a parameter 6 | """ 7 | print("Hello, " + name + ". Good morning!") 8 | greet('Paul') 9 | -------------------------------------------------------------------------------- /functionn.py: -------------------------------------------------------------------------------- 1 | def myFunction() : 2 | return False 3 | 4 | if myFunction(): 5 | print("YES!") 6 | else: 7 | print("NO!") 8 | -------------------------------------------------------------------------------- /globvariable.py: -------------------------------------------------------------------------------- 1 | def myfunc(): 2 | global x 3 | x = "easy" 4 | print("Global!") 5 | 6 | myfunc() 7 | 8 | print("Python is " + x) 9 | -------------------------------------------------------------------------------- /glvariable.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def myfunc(): 4 | print("Python is " + x) 5 | 6 | #myfunc() 7 | 8 | x = "simple" 9 | 10 | def myfunc2(): 11 | x = "easy" 12 | print("Python is " + x) 13 | 14 | myfunc2() 15 | 16 | print("Python is " + x) 17 | -------------------------------------------------------------------------------- /hello.py: -------------------------------------------------------------------------------- 1 | print("Hello, Python!") 2 | -------------------------------------------------------------------------------- /identifyoperator.py: -------------------------------------------------------------------------------- 1 | x1 = 5 2 | y1 = 5 3 | x2 = 'Hello' 4 | y2 = 'Hello' 5 | x3 = [1,2,3] 6 | y3 = [1,2,3] 7 | 8 | # Output: False 9 | print(x1 is not y1) 10 | 11 | # Output: True 12 | print(x2 is y2) 13 | 14 | # Output: False 15 | print(x3 is y3) 16 | -------------------------------------------------------------------------------- /index.py: -------------------------------------------------------------------------------- 1 | b = "Hello, World!" 2 | print(b[-5:-2]) 3 | -------------------------------------------------------------------------------- /indexx.py: -------------------------------------------------------------------------------- 1 | txt = "Hello, welcome to my world." 2 | 3 | x = txt.index("welcome") 4 | 5 | print(x) 6 | -------------------------------------------------------------------------------- /ini.py: -------------------------------------------------------------------------------- 1 | x = 1 2 | y = 35656222554887711 3 | z = -3255522 4 | 5 | print(type(x)) 6 | print(type(y)) 7 | print(type(z)) 8 | -------------------------------------------------------------------------------- /instance.py: -------------------------------------------------------------------------------- 1 | x = 200 2 | print(isinstance(x, int)) 3 | -------------------------------------------------------------------------------- /intro.py: -------------------------------------------------------------------------------- 1 | print("Hello, World!") -------------------------------------------------------------------------------- /introu.py: -------------------------------------------------------------------------------- 1 | print("Hi !!!") 2 | -------------------------------------------------------------------------------- /largestnumber.py: -------------------------------------------------------------------------------- 1 | # Python program to find the largest number among the three input numbers 2 | 3 | # change the values of num1, num2 and num3 4 | # for a different result 5 | num1 = 10 6 | num2 = 14 7 | num3 = 12 8 | 9 | # uncomment following lines to take three numbers from user 10 | #num1 = float(input("Enter first number: ")) 11 | #num2 = float(input("Enter second number: ")) 12 | #num3 = float(input("Enter third number: ")) 13 | 14 | if (num1 >= num2) and (num1 >= num3): 15 | largest = num1 16 | elif (num2 >= num1) and (num2 >= num3): 17 | largest = num2 18 | else: 19 | largest = num3 20 | 21 | print("The largest number is", largest) 22 | -------------------------------------------------------------------------------- /leapyear.py: -------------------------------------------------------------------------------- 1 | # Python program to check if year is a leap year or not 2 | 3 | year = 2000 4 | 5 | # To get year (integer input) from the user 6 | # year = int(input("Enter a year: ")) 7 | 8 | # divided by 100 means century year (ending with 00) 9 | # century year divided by 400 is leap year 10 | if (year % 400 == 0) and (year % 100 == 0): 11 | print("{0} is a leap year".format(year)) 12 | 13 | # not divided by 100 means not a century year 14 | # year divided by 4 is a leap year 15 | elif (year % 4 ==0) and (year % 100 != 0): 16 | print("{0} is a leap year".format(year)) 17 | 18 | # if not divided by both 400 (century year) and 4 (not century year) 19 | # year is not leap year 20 | else: 21 | print("{0} is not a leap year".format(year)) 22 | -------------------------------------------------------------------------------- /loop.py: -------------------------------------------------------------------------------- 1 | for x in "apple": 2 | print(x) 3 | -------------------------------------------------------------------------------- /lower.py: -------------------------------------------------------------------------------- 1 | a = "Hello, World!" 2 | print(a.lower()) 3 | -------------------------------------------------------------------------------- /membershipoperator.py: -------------------------------------------------------------------------------- 1 | x = 'Hello world' 2 | y = {1:'a',2:'b'} 3 | 4 | # Output: True 5 | print('H' in x) 6 | 7 | # Output: True 8 | print('hello' not in x) 9 | 10 | # Output: True 11 | print(1 in y) 12 | 13 | # Output: False 14 | print('a' in y) 15 | -------------------------------------------------------------------------------- /multiline.py: -------------------------------------------------------------------------------- 1 | a = """Python is easy. 2 | Python is simple.""" 3 | 4 | b ='Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium.' 5 | 6 | print(a) 7 | print(b) 8 | -------------------------------------------------------------------------------- /multilinee.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This is a 3 | multiline cooment. 4 | ''' 5 | 6 | print("Hello,Python!") 7 | -------------------------------------------------------------------------------- /multiplevariables.py: -------------------------------------------------------------------------------- 1 | x, y, z = "Orange", "Banana", "Apple" 2 | print(x) 3 | print(y) 4 | print(z) 5 | -------------------------------------------------------------------------------- /multiplicationtable.py: -------------------------------------------------------------------------------- 1 | # Multiplication table (from 1 to 10) in Python 2 | 3 | num = 12 4 | 5 | # To take input from the user 6 | # num = int(input("Display multiplication table of? ")) 7 | 8 | # Iterate 10 times from i = 1 to 10 9 | for i in range(1, 11): 10 | print(num, 'x', i, '=', num*i) 11 | -------------------------------------------------------------------------------- /multivariable.py: -------------------------------------------------------------------------------- 1 | '''Multi Words Variable Names 2 | Variable names with more than one word can be difficult to read. 3 | 4 | There are several techniques you can use to make them more readable''' 5 | 6 | 7 | myVariableName = "Camel Case" 8 | 9 | 10 | MyVariableName = "Pascal Case" 11 | 12 | 13 | my_variable_name = "Snake Case" 14 | 15 | print(my_variable_name , ',' , MyVariableName ,'and' , myVariableName) -------------------------------------------------------------------------------- /number.py: -------------------------------------------------------------------------------- 1 | num = float(input("Enter a number: ")) 2 | if num > 0: 3 | print("Positive number") 4 | elif num == 0: 5 | print("Zero") 6 | else: 7 | print("Negative number") 8 | -------------------------------------------------------------------------------- /number1.py: -------------------------------------------------------------------------------- 1 | num = float(input("Enter a number: ")) 2 | if num >= 0: 3 | if num == 0: 4 | print("Zero") 5 | else: 6 | print("Positive number") 7 | else: 8 | print("Negative number") 9 | -------------------------------------------------------------------------------- /numbers.py: -------------------------------------------------------------------------------- 1 | x = 1 2 | y = 2.8 3 | z = 1j 4 | 5 | print(type(x)) 6 | print(type(y)) 7 | print(type(z)) 8 | -------------------------------------------------------------------------------- /onevalue.py: -------------------------------------------------------------------------------- 1 | x = y = z = "Apple" 2 | print(x) 3 | print(y) 4 | print(z) 5 | -------------------------------------------------------------------------------- /operators.py: -------------------------------------------------------------------------------- 1 | x = 15 2 | y = 4 3 | 4 | # Output: x + y = 19 5 | print('x + y =',x+y) 6 | 7 | # Output: x - y = 11 8 | print('x - y =',x-y) 9 | 10 | # Output: x * y = 60 11 | print('x * y =',x*y) 12 | 13 | # Output: x / y = 3.75 14 | print('x / y =',x/y) 15 | 16 | # Output: x // y = 3 17 | print('x // y =',x//y) 18 | 19 | # Output: x ** y = 50625 20 | print('x ** y =',x**y) 21 | -------------------------------------------------------------------------------- /operators1.py: -------------------------------------------------------------------------------- 1 | x = 10 2 | y = 12 3 | 4 | # Output: x > y is False 5 | print('x > y is',x>y) 6 | 7 | # Output: x < y is True 8 | print('x < y is',x= y is False 17 | print('x >= y is',x>=y) 18 | 19 | # Output: x <= y is True 20 | print('x <= y is',x<=y) 21 | -------------------------------------------------------------------------------- /outvariable.py: -------------------------------------------------------------------------------- 1 | 2 | x = 67459 3 | y = 150675 4 | print(x + y) 5 | 6 | 7 | ''' 8 | x = 6 9 | y = "Joe" 10 | print(x + y) 11 | ''' 12 | -------------------------------------------------------------------------------- /outvariables.py: -------------------------------------------------------------------------------- 1 | x = "simple" 2 | print("Python is " + x) 3 | 4 | #equal to : 5 | 6 | x = "Python is " 7 | y = "simple" 8 | z = x + y 9 | print(z) 10 | -------------------------------------------------------------------------------- /price.py: -------------------------------------------------------------------------------- 1 | txt = "For only {price:.2f} dollars!" 2 | print(txt.format(price = 49)) 3 | -------------------------------------------------------------------------------- /prime.py: -------------------------------------------------------------------------------- 1 | # Program to check if a number is prime or not 2 | 3 | num = 29 4 | 5 | # To take input from the user 6 | #num = int(input("Enter a number: ")) 7 | 8 | # define a flag variable 9 | flag = False 10 | 11 | # prime numbers are greater than 1 12 | if num > 1: 13 | # check for factors 14 | for i in range(2, num): 15 | if (num % i) == 0: 16 | # if factor is found, set flag to True 17 | flag = True 18 | # break out of loop 19 | break 20 | 21 | # check if flag is True 22 | if flag: 23 | print(num, "is not a prime number") 24 | else: 25 | print(num, "is a prime number") 26 | -------------------------------------------------------------------------------- /prime1.py: -------------------------------------------------------------------------------- 1 | # Program to check if a number is prime or not 2 | 3 | num = 407 4 | 5 | # To take input from the user 6 | #num = int(input("Enter a number: ")) 7 | 8 | # prime numbers are greater than 1 9 | if num > 1: 10 | # check for factors 11 | for i in range(2,num): 12 | if (num % i) == 0: 13 | print(num,"is not a prime number") 14 | print(i,"times",num//i,"is",num) 15 | break 16 | else: 17 | print(num,"is a prime number") 18 | 19 | # if input number is less than 20 | # or equal to 1, it is not prime 21 | else: 22 | print(num,"is not a prime number") 23 | -------------------------------------------------------------------------------- /primenumber.py: -------------------------------------------------------------------------------- 1 | # Python program to display all the prime numbers within an interval 2 | 3 | lower = 900 4 | upper = 1000 5 | 6 | print("Prime numbers between", lower, "and", upper, "are:") 7 | 8 | for num in range(lower, upper + 1): 9 | # all prime numbers are greater than 1 10 | if num > 1: 11 | for i in range(2, num): 12 | if (num % i) == 0: 13 | break 14 | else: 15 | print(num) 16 | -------------------------------------------------------------------------------- /quotes.py: -------------------------------------------------------------------------------- 1 | x = "Joe" 2 | #double Single quotes example 3 | x = 'Joe' 4 | 5 | print(x) 6 | -------------------------------------------------------------------------------- /random.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | print(random.randrange(1, 10)) 4 | -------------------------------------------------------------------------------- /remwhitspace.py: -------------------------------------------------------------------------------- 1 | a = " Hello, World! " 2 | print(a.strip()) 3 | -------------------------------------------------------------------------------- /rpps.py: -------------------------------------------------------------------------------- 1 | """Rock, Paper, Scissors 2 | The classic hand game of luck. 3 | Tags: short, game""" 4 | import random, time, sys 5 | print('''Rock, Paper, Scissors 6 | - Rock beats scissors. 7 | - Paper beats rocks. 8 | - Scissors beats paper. 9 | ''') 10 | 11 | # These variables keep track of the number of wins, losses, and ties. 12 | wins = 0 13 | losses = 0 14 | ties = 0 15 | 16 | while True: # Main game loop. 17 | while True: # Keep asking until player enters R, P, S, or Q. 18 | print('{} Wins, {} Losses, {} Ties'.format(wins, losses, ties)) 19 | print("Enter your move: (R)ock (P)aper (S)cissors or (Q)uit") 20 | playerMove = input('> ').upper() 21 | if playerMove == 'Q': 22 | print('Thanks for playing!') 23 | sys.exit() 24 | 25 | if playerMove == 'R' or playerMove == 'P' or playerMove == 'S': 26 | break 27 | else: 28 | print('Type one of R, P, S or Q') 29 | 30 | # Display what the player chose: 31 | if playerMove == 'R': 32 | print('ROCK versus...') 33 | playerMove = 'ROCK' 34 | elif playerMove == 'P': 35 | print('PAPER versus...') 36 | playerMove = 'PAPER' 37 | elif playerMove == 'S': 38 | print('SCISSORS versus...') 39 | playerMove = 'SCISSORS' 40 | 41 | # Count to three with dramatic pauses: 42 | time.sleep(0.5) 43 | print('1...') 44 | time.sleep(0.25) 45 | print('2...') 46 | time.sleep(0.25) 47 | print('3...') 48 | time.sleep(0.25) 49 | 50 | # Display what the computer chose: 51 | randomNumber = random.randint(1, 3) 52 | if randomNumber == 1: 53 | computerMove = 'ROCK' 54 | elif randomNumber == 2: 55 | computerMove = 'PAPER' 56 | elif randomNumber == 3: 57 | computerMove = 'SCISSORS' 58 | print(computerMove) 59 | time.sleep(0.5) 60 | 61 | # Display and record the win/loss/tie: 62 | if playerMove == computerMove: 63 | print('It\'s a tie!') 64 | ties = ties + 1 65 | elif playerMove == 'ROCK' and computerMove == 'SCISSORS': 66 | print('You win!') 67 | wins = wins + 1 68 | elif playerMove == 'PAPER' and computerMove == 'ROCK': 69 | print('You win!') 70 | wins = wins + 1 71 | elif playerMove == 'SCISSORS' and computerMove == 'PAPER': 72 | print('You win!') 73 | wins = wins + 1 74 | 75 | elif playerMove == 'ROCK' and computerMove == 'PAPER': 76 | print('You lose!') 77 | losses = losses + 1 78 | elif playerMove == 'PAPER' and computerMove == 'SCISSORS': 79 | print('You lose!') 80 | losses = losses + 1 81 | elif playerMove == 'SCISSORS' and computerMove == 'ROCK': 82 | print('You lose!') 83 | losses = losses + 1 84 | -------------------------------------------------------------------------------- /rpps_exit.py: -------------------------------------------------------------------------------- 1 | """Rock, Paper, Scissors 2 | The classic hand game of luck. 3 | Tags: short, game""" 4 | import random, time, sys 5 | print('''Rock, Paper, Scissors, 6 | - Paper beats rocks. 7 | - Scissors beats paper. 8 | ''') 9 | 10 | # These variables keep track of the number of wins, losses, and ties. 11 | wins = 0 12 | losses = 0 13 | ties = 0 14 | 15 | while True: # Main game loop. 16 | while True: # Keep asking until player enters R, P, S, or Q. 17 | print('{} Wins, {} Losses, {} Ties'.format(wins, losses, ties)) 18 | print('Enter your move: (R)ock (P)aper (S)cissors or (Q)uit') 19 | playerMove = input('> ').upper() 20 | if playerMove == 'Q': 21 | print('Thanks for playing!') 22 | sys.exit() 23 | 24 | if playerMove == 'R' or playerMove == 'P' or playerMove == 'S': 25 | break 26 | else: 27 | print('Type one of R, P, S or Q') 28 | 29 | # Display what the player chose: 30 | if playerMove == 'R': 31 | print('ROCK versus...') 32 | playerMove = 'ROCK' 33 | elif playerMove == 'P': 34 | print('PAPER versus...') 35 | playerMove = 'PAPER' 36 | elif playerMove == 'S': 37 | print('SCISSORS versus...') 38 | playerMove = 'SCISSORS' 39 | 40 | # Count to three with dramatic pauses: 41 | time.sleep(0.5) 42 | print('1...') 43 | time.sleep(0.25) 44 | print('2...') 45 | time.sleep(0.25) 46 | print('3...') 47 | time.sleep(0.25) 48 | 49 | # Display what the computer chose: 50 | randomNumber = random.randint(1, 3) 51 | if randomNumber == 1: 52 | computerMove = 'ROCK' 53 | elif randomNumber == 2: 54 | computerMove = 'PAPER' 55 | elif randomNumber == 3: 56 | computerMove = 'SCISSORS' 57 | print(computerMove) 58 | time.sleep(0.5) 59 | 60 | # Display and record the win/loss/tie: 61 | if playerMove == computerMove: 62 | print('It\'s a tie!') 63 | ties = ties + 1 64 | elif playerMove == 'ROCK' and computerMove == 'SCISSORS': 65 | print('You win!') 66 | wins = wins + 1 67 | elif playerMove == 'PAPER' and computerMove == 'ROCK': 68 | print('You win!') 69 | wins = wins + 1 70 | elif playerMove == 'SCISSORS' and computerMove == 'PAPER': 71 | print('You win!') 72 | wins = wins + 1 73 | 74 | elif playerMove == 'ROCK' and computerMove == 'PAPER': 75 | print('You lose!') 76 | losses = losses + 1 77 | elif playerMove == 'PAPER' and computerMove == 'SCISSORS': 78 | print('You lose!') 79 | losses = losses + 1 80 | elif playerMove == 'SCISSORS' and computerMove == 'ROCK': 81 | print('You lose!') 82 | losses = losses + 1 83 | -------------------------------------------------------------------------------- /rpps_lz.py: -------------------------------------------------------------------------------- 1 | """Rock, Paper, Scissors 2 | The classic hand game of luck. 3 | Tags: short, game""" 4 | import random, time, sys, os 5 | print('''Rock, Paper, Scissors 6 | - Rock beats scissors. 7 | - Paper beats rocks. 8 | - Scissors beats paper. 9 | ''') 10 | 11 | # These variables keep track of the number of wins, losses, and ties. 12 | wins = 0 13 | losses = 0 14 | ties = 0 15 | 16 | while True: # Main game loop. 17 | while True: # Keep asking until player enters R, P, S, or Q. 18 | print('{} Wins, {} Losses, {} Ties'.format(wins, losses, ties)) 19 | print('Enter your move: (R)ock (P)aper (S)cissors (L)izard (SP)ock or (Q)uit') 20 | playerMove = input('> ').upper() 21 | if playerMove == 'Q': 22 | print('Thanks for playing!') 23 | sys.exit() 24 | 25 | if playerMove == 'R' or playerMove == 'P' or playerMove == 'S' or playerMove == 'L' or playerMove == "SP": 26 | break 27 | else: 28 | print('Type one of R, P, S, L, SP or Q') 29 | 30 | # Display what the player chose: 31 | if playerMove == 'R': 32 | print('ROCK versus...') 33 | playerMove = 'ROCK' 34 | elif playerMove == 'P': 35 | print('PAPER versus...') 36 | playerMove = 'PAPER' 37 | elif playerMove == 'S': 38 | print('SCISSORS versus...') 39 | playerMove = 'SCISSORS' 40 | elif playerMove == 'L': 41 | print('LIZARD versus...') 42 | playerMove = 'LIZARD' 43 | elif playerMove == 'SP': 44 | print('SPOCK versus...') 45 | playerMove = 'SPOCK' 46 | 47 | # Count to three with dramatic pauses: 48 | time.sleep(0.5) 49 | print('1...') 50 | time.sleep(0.25) 51 | print('2...') 52 | time.sleep(0.25) 53 | print('3...') 54 | time.sleep(0.25) 55 | 56 | # Display what the computer chose: 57 | randomNumber = random.randint(1, 5) 58 | if randomNumber == 1: 59 | computerMove = 'ROCK' 60 | elif randomNumber == 2: 61 | computerMove = 'PAPER' 62 | elif randomNumber == 3: 63 | computerMove = 'SCISSORS' 64 | elif randomNumber == 4: 65 | computerMove = 'LIZARD' 66 | elif randomNumber == 5: 67 | computerMove = 'SPOCK' 68 | print(computerMove) 69 | time.sleep(0.5) 70 | 71 | # Display and record the win/loss/tie: 72 | if playerMove == computerMove: 73 | print('It\'s a tie!') 74 | ties = ties + 1 75 | elif playerMove == 'ROCK' and computerMove == 'SCISSORS': 76 | print('You win!') 77 | wins = wins + 1 78 | elif playerMove == 'ROCK' and computerMove == 'LIZARD': 79 | print('You win!') 80 | wins = wins + 1 81 | elif playerMove == 'PAPER' and computerMove == 'ROCK': 82 | print('You win!') 83 | wins = wins + 1 84 | elif playerMove == 'PAPER' and computerMove == 'SPOCK': 85 | print('You win!') 86 | wins = wins + 1 87 | elif playerMove == 'SCISSORS' and computerMove == 'PAPER': 88 | print('You win!') 89 | wins = wins + 1 90 | elif playerMove == 'SCISSORS' and computerMove == 'LIZARD': 91 | print('You win!') 92 | wins = wins + 1 93 | elif playerMove == 'LIZARD' and computerMove == 'SPOCK': 94 | print('You win!') 95 | wins = wins + 1 96 | elif playerMove == 'LIZARD' and computerMove == 'PAPER': 97 | print('You win!') 98 | wins = wins + 1 99 | elif playerMove == 'SPOCK' and computerMove == 'ROCK': 100 | print('You win!') 101 | wins = wins + 1 102 | if wins >= 2: 103 | os.system('echo "SPOCK BEAST MODE ACTIVATED =>" $SECRET') 104 | elif playerMove == 'SPOCK' and computerMove == 'SCISSORS': 105 | print('You win!') 106 | wins = wins + 1 107 | 108 | elif playerMove == 'ROCK' and computerMove == 'PAPER': 109 | print('You lose!') 110 | losses = losses + 1 111 | elif playerMove == 'ROCK' and computerMove == 'SPOCK': 112 | print('You lose!') 113 | losses = losses + 1 114 | elif playerMove == 'PAPER' and computerMove == 'SCISSORS': 115 | print('You lose!') 116 | losses = losses + 1 117 | elif playerMove == 'PAPER' and computerMove == 'LIZARD': 118 | print('You lose!') 119 | losses = losses + 1 120 | elif playerMove == 'SCISSORS' and computerMove == 'ROCK': 121 | print('You lose!') 122 | losses = losses + 1 123 | elif playerMove == 'SCISSORS' and computerMove == 'SPOCK': 124 | print('You lose!') 125 | losses = losses + 1 126 | elif playerMove == 'LIZARD' and computerMove == 'SCISSORS': 127 | print('You lose!') 128 | losses = losses + 1 129 | elif playerMove == 'LIZARD' and computerMove == 'ROCK': 130 | print('You lose!') 131 | losses = losses + 1 132 | elif playerMove == 'SPOCK' and computerMove == 'PAPER': 133 | print('You lose!') 134 | losses = losses + 1 135 | if losses >= 2: 136 | os.system('echo "SPOCK RAGE MODE ACTIVATED =>" $SECRET') 137 | elif playerMove == 'SPOCK' and computerMove == 'LIZARD': 138 | print('You lose!') 139 | losses = losses + 1 140 | -------------------------------------------------------------------------------- /rpps_rigged.py: -------------------------------------------------------------------------------- 1 | import time, sys 2 | print('''Rock, Paper, Scissors 3 | - Paper beats rocks. 4 | - Scissors beats paper. 5 | ''') 6 | 7 | # These variables keep track of the number of wins. 8 | wins = 0 9 | 10 | while True: # Main game loop. 11 | while True: # Keep asking until player enters R, P, S, or Q. 12 | print('{} Wins, 0 Losses, 0 Ties'.format(wins)) 13 | print('Enter your move: (R)ock (P)aper (S)cissors or (Q)uit') 14 | playerMove = input('> ').upper() 15 | if playerMove == 'Q': 16 | print('Thanks for playing!') 17 | sys.exit() 18 | 19 | if playerMove == 'R' or playerMove == 'P' or playerMove == 'S': 20 | break 21 | else: 22 | print('Type one of R, P, S, or Q.') 23 | 24 | # Display what the player chose: 25 | if playerMove == 'R': 26 | print('ROCK versus...') 27 | elif playerMove == 'P': 28 | print('PAPER versus...') 29 | elif playerMove == 'S': 30 | print('SCISSORS versus...') 31 | 32 | # Count to three with dramatic pauses: 33 | time.sleep(0.5) 34 | print('1...') 35 | time.sleep(0.25) 36 | print('2...') 37 | time.sleep(0.25) 38 | print('3...') 39 | time.sleep(0.25) 40 | 41 | # Display what the computer chose: 42 | if playerMove == 'R': 43 | print('SCISSORS') 44 | elif playerMove == 'P': 45 | print('ROCK') 46 | elif playerMove == 'S': 47 | print('PAPER') 48 | time.sleep(0.5) 49 | 50 | print('You win!') 51 | wins = wins + 1 52 | -------------------------------------------------------------------------------- /singledouble.py: -------------------------------------------------------------------------------- 1 | 2 | x = "John" 3 | # is the same as 4 | x = 'John' 5 | 6 | print(x) -------------------------------------------------------------------------------- /slic.py: -------------------------------------------------------------------------------- 1 | b = "Heyo, World!" 2 | print(b[2:]) 3 | -------------------------------------------------------------------------------- /slice.py: -------------------------------------------------------------------------------- 1 | b = "Hello, World!" 2 | print(b[2:5]) 3 | -------------------------------------------------------------------------------- /slicee.py: -------------------------------------------------------------------------------- 1 | b = "Hello, World!" 2 | print(b[:5]) 3 | -------------------------------------------------------------------------------- /stringconcat.py: -------------------------------------------------------------------------------- 1 | 2 | a = "Hello" 3 | b = "Joe" 4 | c = a + b 5 | print(c) 6 | 7 | 8 | a = "Hello" 9 | b = "World" 10 | c = a + " " + b 11 | print(c) 12 | -------------------------------------------------------------------------------- /stringformat.py: -------------------------------------------------------------------------------- 1 | age = 22 2 | txt = "My name is Valentine, and I am {}" 3 | print(txt.format(age)) 4 | -------------------------------------------------------------------------------- /stringrepl.py: -------------------------------------------------------------------------------- 1 | a = "Hello, World!" 2 | print(a.replace("H", "J")) 3 | -------------------------------------------------------------------------------- /strings.py: -------------------------------------------------------------------------------- 1 | print("Hello") 2 | print('Hello') 3 | -------------------------------------------------------------------------------- /stringsplit.py: -------------------------------------------------------------------------------- 1 | a = "Hello, World!" 2 | print(a.split(",")) 3 | -------------------------------------------------------------------------------- /stringss.py: -------------------------------------------------------------------------------- 1 | a = "Hello, World!" 2 | print(a[1]) 3 | -------------------------------------------------------------------------------- /stringvariable.py: -------------------------------------------------------------------------------- 1 | a = "Hello" 2 | print(a) 3 | -------------------------------------------------------------------------------- /strlen.py: -------------------------------------------------------------------------------- 1 | a = "Good Morning!" 2 | print(len(a)) 3 | -------------------------------------------------------------------------------- /syntax.py: -------------------------------------------------------------------------------- 1 | if 5 > 2: 2 | print("Five is greater than two!") 3 | 4 | if 10 > 9: 5 | print("10 is greater than nine!") 6 | 7 | -------------------------------------------------------------------------------- /syntaxx.py: -------------------------------------------------------------------------------- 1 | 2 | x = 10 3 | y = "Hello, World!" 4 | z = 2 5 | 6 | print(z+x) -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | x = "Hello World" 2 | print(len(x)) 3 | 4 | txt = "Hello World" 5 | y = txt[0] 6 | 7 | txt = "Hello World" 8 | z = txt[2:5] 9 | 10 | txt = " Hello World " 11 | a = txt.strip() 12 | 13 | txt = "Hello World" 14 | b = txt.upper() 15 | 16 | txt = "Hello World" 17 | c = txt.lower() 18 | 19 | txt = "Hello World" 20 | d = txt.replace("H", "J") 21 | 22 | age = 25 23 | txt = "My name is Joe, and I am {}" 24 | print(txt.format(age)) 25 | -------------------------------------------------------------------------------- /true.py: -------------------------------------------------------------------------------- 1 | a = 200 2 | b = 33 3 | 4 | if b > a: 5 | print("b is greater than a") 6 | else: 7 | print("b is not greater than a") 8 | -------------------------------------------------------------------------------- /type.py: -------------------------------------------------------------------------------- 1 | x = int(1) 2 | y = int(2.8) 3 | z = int("3") 4 | 5 | a = float(1) 6 | b = float(2.8) 7 | c = float("3") 8 | d = float("4.2") 9 | 10 | e = str("s1") 11 | f = str(2) 12 | g = str(3.0) 13 | 14 | print(a) 15 | print(b) 16 | print(c) 17 | print(d) 18 | print(e) 19 | print(f) 20 | print(g) 21 | print(x) 22 | print(y) 23 | print(z) 24 | -------------------------------------------------------------------------------- /typeOf.py: -------------------------------------------------------------------------------- 1 | 2 | x = 5 3 | y = "John" 4 | print(type(x)) 5 | print(type(y)) -------------------------------------------------------------------------------- /typeof.py: -------------------------------------------------------------------------------- 1 | x = 10 2 | y = "Joe" 3 | print(type(x)) 4 | print(type(y)) 5 | -------------------------------------------------------------------------------- /unpack.py: -------------------------------------------------------------------------------- 1 | fruits = ["Orange", "Banana", "Cherry"] 2 | x, y, z = fruits 3 | print(x) 4 | print(y) 5 | print(z) 6 | -------------------------------------------------------------------------------- /uppercase.py: -------------------------------------------------------------------------------- 1 | a = "Hello, World!" 2 | print(a.upper()) 3 | -------------------------------------------------------------------------------- /var.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | y = "Joe" 3 | print(x) 4 | print(y) 5 | 6 | a = 4 7 | a = "John" 8 | print(a) 9 | -------------------------------------------------------------------------------- /variaable.py: -------------------------------------------------------------------------------- 1 | myvar = "Joe" 2 | my_var = "Joe" 3 | _my_var = "Joe" 4 | myVar = "Joe" 5 | MYVAR = "Joe" 6 | myvar2 = "Joe" 7 | 8 | print(myvar) 9 | -------------------------------------------------------------------------------- /variable.py: -------------------------------------------------------------------------------- 1 | '''Variable Names 2 | A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: 3 | A variable name must start with a letter or the underscore character 4 | A variable name cannot start with a number 5 | A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) 6 | Variable names are case-sensitive (age, Age and AGE are three different variables)''' 7 | 8 | 9 | myvar = "John" 10 | my_var = "John" 11 | _my_var = "John" 12 | myVar = "John" 13 | MYVAR = "John" 14 | myvar2 = "John" 15 | 16 | print(myvar) 17 | 18 | ''' 19 | Illegal variable names: 20 | 2myvar = "John" 21 | my-var = "John" 22 | my var = "John" 23 | ''' -------------------------------------------------------------------------------- /variables.py: -------------------------------------------------------------------------------- 1 | 2 | x = 5 3 | y = "John" 4 | print(x) 5 | print(y) 6 | 7 | a = 4 8 | a = "Sally" 9 | print(a) -------------------------------------------------------------------------------- /vars.py: -------------------------------------------------------------------------------- 1 | myVariableName = "Camel Case" 2 | 3 | 4 | MyVariableName = "Pascal Case" 5 | 6 | 7 | my_variable_name = "Snake Case" 8 | 9 | print(my_variable_name) 10 | print(MyVariableName) 11 | print(myVariableName) 12 | -------------------------------------------------------------------------------- /whitespace.py: -------------------------------------------------------------------------------- 1 | WhiteTabs = "H\te\tl\tl\to" 2 | 3 | x = WhiteTabs.expandtabs(2) 4 | 5 | print(x) 6 | --------------------------------------------------------------------------------