October 29, 2017 at 4:13 pm #716
In this post we will be looking at the very basics of programming in python. Python is an excellent beginner programming language. It is easy to read and can make use of lots of different library’s allowing you to code basic computer programs, bots, websites and even can allow you to make super advanced machine learning programs.
Calculations in python are written just like they are on a normal calculator for example
>>> 18//4 #Using two // rounds the number down to the nearest whole number.
>>> 5**3 #Easier way of calculating power numbers
>>> apple =4 #These are variables and can store data which then can be added together with other variables.
>>> orange = 3
#Basic “Strings” in python
>>> ‘string’ #A string can be contained within two ”
>>> “hello my name is james” #You can also contain a string in two “” instead
‘hello my name is james’
>>> ‘I do\’nt like pie’ #If you have a ‘ in your string you must escape it by placing a \ in front of it so python doesn’t get confused.
“I do’nt like pie”
#First function to print string use print function and place string.
>>> print(“hey there people”)
hey there people
#You can also add strings together for example:
>>> firstName =”james ” #Note the space added to end of string else first and last name will be merged together
>>> lastName =”marcus”
>>> firstName *5 #This will display the first name 5 times!
‘james james james james james’
>>> user =”James Howard”
>>> user #this gets the 5th character in a string Computer start counting at 0 so for example 0 will be J 1 will a… etc. A blank space will also count a character, so fetching the fifth character will show a blank space.
>>> user[-1] #if you want to fetch the last character of a file use this function
>>> user[-4] #If you want to fetch the fourth last character use this.
>>> user[2:7] #This code will fetch all characters from the second character up to the seventh character.
>>> user[::7] #if you want to echo out the first character with the seventh character use this.
>>> user[:5] #this will fetch the characters from the start to the fifth character.
>>> user[2:] #this will fetch the characters starting from the second character to the end of the string.
#Using this function will help you find the character length of a string.
#You can input a variable into the function to count the number of charterers in a string stored by that variable.
Lists in python are very important and are very easy to do!
>>> Numberlist = [12, 22, 33, 11, 18] #This will create a list of different numbers
>>> Numberlist  #This will fetch the thrid item on the list, remember that the computer starts counting at 0.
>>> Numberlist=39 #You can temporary change a number in the list by using this code.
>>> Numberlist + [1000,1002,10004] #You can then temporary append extra numbers to your list using this code.
[12, 22, 39, 11, 18, 1000, 1002, 10004]
>>> Numberlist.append(10000) #To permanently append extra numbers onto your list which will not get overwritten use .append
[12, 22, 39, 11, 18, 10000]
>>> Numberlist[:2] #This will fetch the first two items in the list.
>>> Numberlist [:2] =[0,0] #This will change the the first two items in the list to 0
[0, 0, 39, 11, 18, 10000]
>>> Numberlist [:2] =  #This will delete the first two items in the list forever!
[39, 11, 18, 10000]
>>> Numberlist[:] = #This code will fetch all characters from beginning to end and will then set them to nothing deleting everything.
>>> Numberlist #As you can see nothing is returned.
You must be logged in to reply to this topic.