What is python
Python is an interpreted, object oriented, high level programming language with dynamic semantics. You might think that programming languages are abit like space rockets in that they are designed by white-coated scientists with mega-brains who get everything right the first time and always produce perfect solutions. However, this is not the case. Programming languages have been developed over the years for all kinds of reasons, including reasons as simples as "it seemed like a good idea at the time."
Guido van Rossum, the original developer of python had no idea just what he was starting when, in late 1989, he decided to spend a few weeks on a hobby programming project that he could work on while his office at work was closed during Christmas. He made this language" python" not because of a liking for snakes, but because he was an enthusiastic fan of the British TV comedy shows Monty python's flying circus. However, the language was picked up by programmers the world over who loved its elegance and power. Python is now one of the most popular programming language in use today.
Getting started
We will start by going to https://www.python.org/downloads/ using our browser we look for our preferred python version for your Os, that is if you're using macOS, Linux/UNIX, windows and others. Download, configure and run the setup, then you will start the python environment and interact with it.The tool you'll use is called the IDLE which means "integrated development learning Environment". It provides two ways of interacting with python: the "shell" where you can type python commands that are executed instantly, and a text editor that's allows you to create program code documents.
A Good first program
Lets start with a simple "Hello world" program.
print("Hello world!")
Print("Hello Again")
print("I like typing this.")
print("This is fun.")
print("yay! printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')
Comments
Comments are very important in your programs. They are used to tell you what something does in English, and they are used to disable parts of your program if you need to remove them temporarily. Here is how you use comments in python
we use # comment, this is so you can read your program later.
Anything after the # is ignored by python.
You can also use a comment to "disable" or comment out code.
print("I could have code like this.") #and the comment after is ignored.
Numbers and math
Every programming language has some kind of way of doing numbers and math. Here are some of the names:
+plus,-minus,/slash,*asterisk,% percent,greater than,<= less-than-equal,>=greater-than-equal.
Variables
Is a symbolic name that is used as a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name. Example
my_height="50"
Types of variables
Strings-A string is usually a bit of text you want to display to someone or "export" out of the program you are writing. Python knows you want to make something a string when you put either"(double-quotes) or (single-quotes) around the text.
Numbers: we have three numerical types.
Integer-This gives a whole number without decimal places.
Example: r=654Float-These are numbers that have fractional parts.
Example: u =68.9Booleans- This allow an output to give an true or false answer.
Example: True or FalseComplex-Uses "j" as an imaginary part.
Example: y=45r
Lists
Lists can be sequences of arbitrary values. They can also contain numbers, strings, or both!
You can also call a list's contents using indices my_list[2] this will give the output of the string at index 2.
Example:
mylist= [1,"spam",4,"U"]
functions
A function is a sub-program, a small inside of a program. The basic idea is we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name.
The part of the program that creates a function is called a function definition. When the function is used in a program, we say the definition is called or invoked. Example
def main()
print("happy birthday to you!")
print("happy birthday to you!")
print("happy birthday, dear Fred")
print("happy birthday to you!")
main()
Kindly note when using an interactive environment the code will not appear as type above.
Sets
A set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. Python's set class represents the mathematical notion of a set. Example
Set1={"mammals","reptiles","birds","amphibians"}
print(Set1)
Dictionaries
Can be created in python by listing key-values pairs inside of curly braces. Key and values are joined by ":" and separated with commas.
[] return the objects with the associated key. Dictionaries are mutable. Example: of an empty collection adding key value pairs at a go.
passwd={}
for line in open('passwords','r'):
user,pass=line.split()
passwd[user]=pass
Loops
It iterates over an object until that object is complete. We have several types of loops as follows:
-
For loop-The statement allow us to iterates through a sequence of values. Example
for i in range(5):
print(i)
Kindly note the code will not appear as typed above in an interactive environment.
-
While loop-Semantically the body of the loop executes repeatedly as long as the condition remains true. When its false the condition terminates. Example
i=0
while i<=10
print(i)
i=i+1
kindly note that in an interactive environment the code will not appear the same.
- If else-Evaluates whether an expression is true or false. If the condition is true the if statement executes. Otherwise the "else" statements executes. Example: name=Jason if name=='Jason' print("Hello Jason, welcome") else: print("sorry, I don't know you")
Tuples
Is a collection of python separated by commas. In some ways a tuple is similar to a list in terms of indexing, nested objects and repetition but a tuple is immutable unlike lists. Example
my_tuple=("Dinosaur"200)
print(my_tuple)
Coding is fun when you're interested and committed.
resources from: How to learn python the hard way.
Cheers
Top comments (0)