20 January 2015

Fun With Python 2

Functions are a huge part of coding. If these weren't used, programmers would need to write codes that were extremely long and messy. 

Here's a simple function :

This means that I defined (def) my function named greeting as such. It takes in one parameter, name, and then goes through the rest of itself. 
First, it prints a string 'Hello, ', and then prints the name that if was given, and then some exclamation points. 
Then, it asks what the date is, and stores the user's answer as date.
Then, it prints the last string, addresses the user, and prints the date that the user input. 




The first line is calling the function greeting with 'Cici' input as the parameter, name.
It prints the first string and addresses the user.
Asks for the date, and then the user replies.
Then, gives a goodbye salutation to the user.


Another very simple program is this one :


This program prompts the user to enter 10 numbers.
count is a variable that is initialized as 1. 
The while refers to a loop called the while loop. Basically, this is saying , while something is true, do something. So, while count is less than 11, do the things that are inside the loop, which in this case is the rest of the function, as recognizable by the indentation.
The last line is saying that now, count is equal to itself plus 1. So the variable count is just counting how many times the loop runs and the loop will stop once it has run ten times. 




Here is one more example of a function. This one just tells you whether a number is even or odd.

Here, I used the modulo, or %. So, if the number that was input is divided by 2 and there isn't a remaining number, the number must be even. Like, 8 / 2 = 4, remainder 0. 7 / 2 = 3, remainder 1.

The if and else are an if/else statement, which is basically, if something is true, do this, otherwise (or else) do this. So, the number will be divided by two, and if there isn't a remainder, the program will print 'Even. '. If this is not true, and there is a remainder, the program will print 'Odd. '.





Farewell pic :

Lucy running.


                                                                                         ~ Cici


1 comment:

  1. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from Pprogramming

    ReplyDelete