diff --git a/EscapeChar.py b/EscapeChar.py new file mode 100644 index 0000000..f0095aa --- /dev/null +++ b/EscapeChar.py @@ -0,0 +1,40 @@ +# Use "\n" to split strings across multiple lines + +splitString = "This string has been\nsplit over\nseveral\nlines" +print(splitString) + +# Use "\t" to tab contents of a string +tabbedString = "1\t2\t3\t4\t5" +print(tabbedString) + +# Backslashes can also be used to escape special characters such as quotes and double quotes +print('The pet shop owner said "No, no, \'e\'s uh,....he\'s resting".') +# Or +print("The pet shop owner said \"No, no, 'e's uh,... he's resting\".") + +# You can also use triple quotes for strings + +print("""The pet shop owner said "No, no, 'e's uh,...he's resting". """) + + +# Additionally, you can split strings across multiple lines by writing them over multiple lines +# This split can be cancelled by using the the backslash (\) as well. +anotherSplitString = """This string has been \ +split over \ +several \ +lines""" + +print(anotherSplitString) + +# If you would like to include backslashes in your strings you will need to either +# prefix the string with an r, or put an addtional backslash in the string. +print('Example without r or \\\\: ', "C:Users\etc\notes.txt") + +print("Examples with prefixing string with r or adding double backslashes") +# The 'r' stands for raw string +print("C:Users\\etc\\notes.txt") +print(r"C:Users\etc\notes.txt") + +input() + + diff --git a/HelloWorld.py b/HelloWorld.py new file mode 100644 index 0000000..dfe74a0 --- /dev/null +++ b/HelloWorld.py @@ -0,0 +1,14 @@ +print('Hello, World!') +# In this case, "hello world" is a string argument and print is the function. +# We are passing the argument into the function + +print(1+2) +print(7*6) +print() + +# When calling a function, you must have paranthesis after a function name. +# The Print function can take different kind of arguments + +print('The End', 'or is it?',) + +input() diff --git a/Operators.py b/Operators.py new file mode 100644 index 0000000..4f7cff5 --- /dev/null +++ b/Operators.py @@ -0,0 +1,28 @@ +a = 12 +b = 3 + +# An expression in python is anything that can be calculated to return a value +print(a + b) +print(a - b) +print(a * b) +print(a / b) +print(a // b) #integer division, rounded down towards minus infinity +print(a % b) # 0 modulo: the remainder after integer division + +print() + +for i in range(1, a // b): + print(i) + +# Python follows PEMDAS +# Python allows use of parenthesis with expressions +print(a + b / 3 - 4 * 12) +print(a + (b / 3) - (4 *12)) +print ((((a + b) / 3) - 4) * 12) + +c = a + b +d = c / 3 +e = d - 4 +print(e * 12) + +input() diff --git a/README.md b/README.md new file mode 100644 index 0000000..b1c792d --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Python-Notes +My notes on the python programming language. + +These note are for begginners, like myself to refer to. These notes can be redistrubuted or changed in every way so long as they do +not mislead other learners. These notes are completely free and will be updated as I learn more of the python programming language. +If you see something that is incorrect, inaccurate or could just be worded better, please let me know and I will make the necessary changes. +Everyone is welcome to download or add to this repository. I encourage it :) diff --git a/SliceBack.py b/SliceBack.py new file mode 100644 index 0000000..e7286c4 --- /dev/null +++ b/SliceBack.py @@ -0,0 +1,25 @@ +letters = "abcdefghijklmnopqrstuvwxyz" + +backwards = letters[25:0:-1] +print(backwards) + +# Prints all letters of the alphabet backwards except for A +# This is because we used the stop value of 0 and slices +# extend up to but do not include the stop value + +# CHALLENGE +# Using the letters string from the video, add some code to create the following slices. +# Create a slice that produces the characters qpo +# Slice the string to produce edcba +# Slice the string to produce the last 8 characters, in reverse order. + +slice1 = letters[16:13:-1] +print(slice1) + +slice2 = letters[4::-1] +print(slice2) + +slice3 = letters[26:17:-1] +print(slice3) + +input() \ No newline at end of file diff --git a/StringFormatting.py b/StringFormatting.py new file mode 100644 index 0000000..9e3087d --- /dev/null +++ b/StringFormatting.py @@ -0,0 +1,40 @@ +for i in range(1, 13): + print("No. {0} squared is {1} and cubed is {2}".format(i, i ** 2, i ** 3)) + + +#We can clean up the above line of code by inputting a colon followed by applying a field width +print(" ") +print("Clean Version") +for i in range(1, 13): + print("No. {0:2} squared is {1:4} and cubed is {2:4}".format(i, i ** 2, i ** 3)) + + +#The Lines of code below left-align the output using the less-than sign < +print(" ") +print("Cleaner") +for i in range(1, 13): + print("No. {0:2} squared is {1:<3} and cubed is {2:<4}".format(i, i ** 2, i ** 3)) + +#We can also center the output of the code by using a ^ instead of a < +print(" ") +print("Mr. Clean") +for i in range(1, 13): + print("No. {0:2} squared is {1:^3} and cubed is {2:^4}".format(i, i ** 2, i ** 3)) + +# We can specify the amount of numbers shown after a decimal by using the the f function, preceeded by a number +print("") +print("Floating point format") +print("The value of pi is {0:12}".format(22/7)) +print("The value of pi is {0:12f}".format(22/7)) +print("The value of pi is {0:12.50f}".format(22/7)) +print("The value of pi is {0:52.50f}".format(22/7)) +print("The value of pi is {0:62.50f}".format(22/7)) +print("The value of pi is {0:72.50f}".format(22/7)) +#The maximum precision that can be defined in python is between 51 and 53 numbers +#I know that the value of pi is off. Don't harp on me + + +#Another way of formating numbers and strings is using "f" as represented below. +print(f"Pi is approximately {22 / 7:12.50f}") + +input() \ No newline at end of file diff --git a/StringOperators.py b/StringOperators.py new file mode 100644 index 0000000..198840a --- /dev/null +++ b/StringOperators.py @@ -0,0 +1,71 @@ +# String Operators + +#Python 3 has 5 sequence types built in: +# The string type +# list +# tuple +# range +# bytes and bytearray + +# What is a sequence? + +# a sequence is defined as an ordered set of items. +# For example, the string "hello world contains 11 items, and each item is a chracter. +# A list is also a sequence type. For example, here's a python list of things you might need when buying a new computer: +# ["Computer", "monitor", "keyboard", "mouse", "mouse mat"] +# That list conatins 5 items, each of which is a string. +# in other words, the example was a sequence where each item is also a sequence +# Be sure to fully understand indexing, since it is very important when dealing with sequences. + +# Because a sequence is ordered, we can use indexes to acces indevidivual items in the sequence. + +computer_parts = ["Computer", "monitor", "keyboard", "mouse", "mouse mat"] +print(computer_parts[1]) # Prints out "Monitor" + +# That is also a sequence that we can index into as well +print(computer_parts[1][0]) # Prints out the letter "m" + +# Everything that you can do with the str sequence type can also be done with the other sequence types. +# One exception though, not all sequence types can be concatenated or multiplied. range is an example. + +# Sequence operators + +string1 = "he's" +string2 = "probably" +string3 = "pining" +string4 = "for the" +string5 = "fjords" + +print(string1 + string2 + string3 + string4 + string5) # The plus is not necessary when concatenating string literals in python + +print("he's " "probably " "pining " "for the " "fjords") + +# Strings can also be multiplied + +print("Hello " * 5) # Prints hello 5 times + +# Lists and tuples can also be multiplied +# Order of Operations is also applied when multiplying strings + +# Checking for values in varaiables +today = "friday" +print("day" in today) #true +print("fri" in today) #true +print("thur" in today) #false +print("parrot" in "fjord")#false + + + + + + + + + +# String Replacement Fields. + + + + + +input() \ No newline at end of file diff --git a/StringReplacementFields.py b/StringReplacementFields.py new file mode 100644 index 0000000..0106b00 --- /dev/null +++ b/StringReplacementFields.py @@ -0,0 +1,21 @@ +# When printing strings and numbers, it would often be convenient to display both values +# using a single call to print. For example, we may want to print a description of what a value is, before the value itself. +# Numbers can't be concatenated with strings using +. +# Instead we can use the str function in python to couerce our numbers into a string. + +age = 22 +print("I am " + str(age) + " years old") + +# The example below is another method of combining strings with numbers. +print("I am {0} years old".format(age)) + +#Another Example: +days = 31 +print("There are {0} days in {1}, {2}, {3}, {4}, {5}, {6} and {7}".format(days, "Jan", "March", "May", "Jul", "Aug", "Oct", "Dec")) + +# The example above provides format for using replacement fields. Replacement fields are defined by +# curly braces {} containing the number of a position, much like arrays. These position numbers are then defined +# by the .format() function, which will contain data inside the paranthesis, much like the line of code above. + + +input() \ No newline at end of file diff --git a/Strings.py b/Strings.py new file mode 100644 index 0000000..fd7f7ab --- /dev/null +++ b/Strings.py @@ -0,0 +1,33 @@ +print("Today is a good day to learn Python") +print('Python is fun') +print("Python's strings are easy to use") +print('We can even include "quotes" in strings') +# You can use single or double quotes when printing strings +# However, when you start a string with one type of quote, you +# must finish the string with the same type of quote + +# You can also concatanate strings using the plus sign: +print("Hello" + " world") + +greeting = "Hello" +name = input("Please enter your name: ") +# The "input" function diplays the text provided to it, and then waits for user input +# to be entered. It then stores the user input into the "name" variable + +print(greeting + ' ' + name) + +# A variable is basically just a way to give a (meaningful name to an area of memory, into +# which we can place certain values. + +# Python variable names must begin with a laetter (either upper or lower case or an underscore_character. +# They can contain letters, numbers or underscore characters ( but cannot begin with a number). +# Python variables are case sensitive + +age = 21 +print(age) + +# "type" stands for data type and describes the kind of information we are storing +print(type(greeting)) +print(type(age)) + +input() \ No newline at end of file diff --git a/Strings2.py b/Strings2.py new file mode 100644 index 0000000..adfd9ae --- /dev/null +++ b/Strings2.py @@ -0,0 +1,128 @@ +# Strings are one of pythons sequence data types +# 01234567890123 +parrot = "Norwegian Blue" + +print(parrot) + +# Use brackets in an expression to print a specific character from a string +# Ex: parrot[3] will print the "w" character since the character count starts at 0 +# with N in "Norwegian Blue" being 0, O being 1, R being 2 and W being 3 +print(parrot[3]) + +# Mini Challenge: +# Add some code ot the program, so that it prints out "we win" +# Each character should appear on a separate line. +# The program should get the characters from the parrot string, using indexing. +# The w is already printed out, you just need to print the remaining 5 characters. +# With the text that is already being printed, the final output from the program should be: +# Norwegian Blue +# w +# e +# +# w +# i +# n + +print(parrot[4]) +print() +print(parrot[3]) +print(parrot[6]) +print(parrot[8]) + +# You can index backwards as well by using a "-" symbol +print() + +print(parrot[-11]) +print(parrot[-10]) +print() +print(parrot[-11]) +print(parrot[-8]) +print(parrot[-6]) + + + +# Slicing +# The expression below is asking python to start at position 0 and slice all the way +# up to, but not including position 6 +# Last character is not included in slice +print(parrot[0:6]) #Norweg +print(parrot[0:5]) #we +print(parrot[0:9]) #Norwegian +print(parrot[:9]) # Same as starting from 0 + +print(parrot[10:14]) +#or +print(parrot[10:]) +# Same rules apply for using a "-" sign as well +print(parrot[-4:-2]) +print(parrot[-4:12]) + +# Using a step in a slice + +print(parrot[0:6:2]) #Nre +#The slice start at index postion 0. +#it extends up to (but not including) position 6. +#We step through the sequence in steps of 2 + +print(parrot[0:6:3]) #Nw +# The slice starts at index position 0. +#it extends up to (but not inlcuding) position 6. +#We step through the sequence in steps of 3 + +number = "9,223,372,036,854,775,807" +print(number[1::4]) +# Starts at position 1 and slices every 4th character +# Omitting the middle number allows the slice to go all the way through the variable + + +# Slicing backwards + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +input() \ No newline at end of file