Posted in Tech

A quick guide on the basics of Python

Most people who wish to learn a programming language often cannot find the time or lack the facilities or opportunities. For the past eight years or so, I’ve had such difficulties that obstructed me from mastering one. It wasn’t until last month that I could actually learn python and I figured that I could post some of the things that I learned in the form of a guide.

Before we start, know that a string is line of characters. example – “Hello”

So the first thing one must learn to do when learning a programming language is how to display text using python, i.e. we use the print() method

syntax 
1 print("hello world")
2 print(23)  <-- the 23 if put like this ->("23") becomes a *string
3 print(2 + 3) 
4 print("2"+"3")<--(these nos. are nos. strings(gets *concatenated) 

output
1 Hello World
2 23 

You can assign word(s) or variables . They are called to display a certain string or integer etc.

syntax
1 text = " Hello World"
  print(text)
2 text = print("Hello World")

output
1 Hello World
2 Hello World

String concatenation is useful when you want to merge different strings together.

syntax
1 fruit = "Apples"
  statement = " are nice!"
  print (fruit + statement)

output
1 Apples are nice!

You can also put items in a list and pull out and add more as you go.

Here you have an empty list:

syntax
1 books = []

To add items to the list we use the .append method :

syntax
1 books = []
  books.append("Harry Potter")
  books.append("Wimpy Kid")
  print(books)

output
1 ["Harry Potter" , "Wimpy Kid"]

Another thing to keep in mind is that in a string or list for example ,every piece (word or no. etc) has it’s own index or position no. For example, in “Wimpy Kid” Both “Wimpy” and “Kid” have their own index no. Also remember that the count starts from 0 and not from 1 therefore “Wimpy” has the index no. 0 and so on. this can be applicable to just one word where index positions are alloted to specific characters and even in numericals. the index no. is given in this –> []

syntax
1 text = "Hello World"
  print(text[1])
2 word = "littlebanyantreekids"
  print(word[6])
3 number = 79936429
  print(number[4])
4 sentence = "You are awesome"
  print(sentence[-1])
  print(sentence[-2] 

output
1 Hello
2 b
3 6
4 awesome
  are

Index position [-1] is possible and is the last word character or numerical in the string or integer.

You can use index positions to get data from a list as well:

syntax 
1 mylist = ["books","games","movies","programming"]   print(mylist[3]) 

output
1 programming

You can also pick up more than one item by adding a stop index to the start one , [0:2]. The first no. indicates which index position to begin from and the second indicated where it should stop. note, that the no. in the stop index is not counted therefore goes only up to but but excudes it therefore in this case only only index[0] and index[1] are taken and not index[2].

input
1 names = ['Sam', 'Bob', 'Adam', 'Alice'] 
  print(names[:2])

output
1 ["Sam" , "Bob"]
  

When assigning a new list by picking entries from another list it is called slicing:

syntax
input
1 names = ['Sam', 'Bob', 'Adam', 'Alice'] 
 siblings = names[:2]

output
1 ["Sam" , "Bob"]

Here’s an example of how we can manipulate and play with numbers using lists

syntax
1 squares = [] 
  for number in range(1, 11): 
     squares.append(number**2)  <-------(indent is important)
  print(squares)

output
[1,4,9,16,25,36,49,64,81,100,121]

What the code above implies is that : 1. squares is an empty list . For every number from a range of 1 to 11, square the number and append it to squares (the list).

List comprehensions are a shorter way but not necessarily most readable ways to create lists the objective if used is to minimise the space taken up by code:

syntax
1 squares = [number**2 for number in range(1, 11)]  
  print(squares)

output
[1,4,9,16,25,36,49,64,81,100,121]

And that is all that we have for this months edition. More from me next month. I hope you found this useful . πŸ™‚

Advertisement

Author:

I am a friendly person with a helping nature. I am a huge fan of robotics and wish to pursue this in the future. For now I plan to ace my 10th grade and keep writing new blogs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s