Before we move on I'd like to introduce Geany, which is a simple text editor that lets you run almost all of your programs directly from the editor. It also displays your output in a terminal window,
which helps you get comfortable using terminals.
Windows installer for Geany by going to http://www.geany.org/ and clicking Releases in the Download menu. Run the installer called geany-1.33_setup.exe, or something similar, and accept all of the defaults.
Then open Geany editor from your desktop....
Now open you first python program using geany, file-> open ->location of your file on computer
Here I'd recommend to create a folder for you Python programs and store all the projects and programs in this folder only. I've created python_code folder and a sub folder examples in it.
Once you open your first program you can see how neat this editor looks..
Next step would be to compile the program and run, commands are on the ribbon area of the editor. The output can be seen on a new terminal window.
When you run the file First_program.py, the ending .py indicates that the file is a Python program. Your editor then runs the file through the Python interpreter, which reads through the program and determines what each word in the program means. For example, when the interpreter sees the word print, it prints to the screen whatever is inside the parentheses.
As you write your programs, your editor highlights different parts of your program in different ways. For example, it recognizes that print is the name of a function and displays that word in blue. It recognizes that “Hello world” is not Python code and displays that phrase in orange. This feature is called syntax highlighting and is quite useful as you start to write your own programs.
Variables
A variable is a name that refers to a value. A variable provides a way to label and access information. Instead of having to know exactly where in the computer’s memory some information is stored, you use a variable to get at it.
It’s kind of like calling your friend on his cell phone. You don’t have to know where in the
city your friend is to reach him. You just press a button and you get him.
An assignment statement creates new variables and gives them values.
Now it's time to learn about variables in Python. Let's modify our First_program.py file and Add a new line at the beginning of the file, and modify the second line:
greeting = "Welcome to the world of Python!"
print(greeting)
Save this file as second_program.py, compile and run. Your output should look like
We’ve added a variable named greeting . Every variable holds a value, which is the information associated with that variable. In this case the value is the text "Welcome to the world of Python!"
Adding a variable makes a little more work for the Python interpreter. When it processes the first line, it associates the text "Welcome to the world of Python!" with the variable greeting . When it reaches the second line, it prints the value associated with message to the screen.
Now add the following lines in your program file (insert a couple of empty lines to separate the codes by pressing enter key from your keyboard)-
greeting = "Python is easy to master!"
print(greeting)
Now run second_program.py file. Your output should have two lines, something like -
We can change the value of a variable in your program at any time, and Python will always keep track of its current value.
One must adhere to a few rules and guidelines when using variables in Python. Here are some of the rules-
• Variable names can contain only letters, numbers, and underscores. They can start with a letter or an underscore, but not with a number. For instance, you can call a variable var_1 but not 1_var.
• Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, first_name works, but first name will cause errors.
• Avoid using Python keywords and function names as variable names; that is, do not use words that Python like other languages has reserved for a particular programmatic purpose, such as the word print.
• Variable names should be short but descriptive. For example, email is better than em, first_name is better than f_n, and word_length is better than length_of_the_words.
• Be careful when using the lowercase letter l and the uppercase letter O because they could be confused with the numbers 1 and 0.
Python is dynamically-typed, meaning that no pre-declaration of a variable or its type is
necessary. The type (and value) are initialized on assignment. Assignments are performed
using the equals sign.
As an exercise you may try to create a few programs, say -
1. store a message in a variable and then print that message
2. Store a message in a variable, and print that message. Then change the value of your variable to a new message, and print the new message.
3.What if the first print statement is removed from the program? Just try and see the result.
We'll continue with this topic in the next blog.
which helps you get comfortable using terminals.
Windows installer for Geany by going to http://www.geany.org/ and clicking Releases in the Download menu. Run the installer called geany-1.33_setup.exe, or something similar, and accept all of the defaults.
Then open Geany editor from your desktop....
Now open you first python program using geany, file-> open ->location of your file on computer
Here I'd recommend to create a folder for you Python programs and store all the projects and programs in this folder only. I've created python_code folder and a sub folder examples in it.
Once you open your first program you can see how neat this editor looks..
Next step would be to compile the program and run, commands are on the ribbon area of the editor. The output can be seen on a new terminal window.
When you run the file First_program.py, the ending .py indicates that the file is a Python program. Your editor then runs the file through the Python interpreter, which reads through the program and determines what each word in the program means. For example, when the interpreter sees the word print, it prints to the screen whatever is inside the parentheses.
As you write your programs, your editor highlights different parts of your program in different ways. For example, it recognizes that print is the name of a function and displays that word in blue. It recognizes that “Hello world” is not Python code and displays that phrase in orange. This feature is called syntax highlighting and is quite useful as you start to write your own programs.
Variables
A variable is a name that refers to a value. A variable provides a way to label and access information. Instead of having to know exactly where in the computer’s memory some information is stored, you use a variable to get at it.
It’s kind of like calling your friend on his cell phone. You don’t have to know where in the
city your friend is to reach him. You just press a button and you get him.
An assignment statement creates new variables and gives them values.
Now it's time to learn about variables in Python. Let's modify our First_program.py file and Add a new line at the beginning of the file, and modify the second line:
greeting = "Welcome to the world of Python!"
print(greeting)
Save this file as second_program.py, compile and run. Your output should look like
We’ve added a variable named greeting . Every variable holds a value, which is the information associated with that variable. In this case the value is the text "Welcome to the world of Python!"
Adding a variable makes a little more work for the Python interpreter. When it processes the first line, it associates the text "Welcome to the world of Python!" with the variable greeting . When it reaches the second line, it prints the value associated with message to the screen.
Now add the following lines in your program file (insert a couple of empty lines to separate the codes by pressing enter key from your keyboard)-
greeting = "Python is easy to master!"
print(greeting)
Now run second_program.py file. Your output should have two lines, something like -
We can change the value of a variable in your program at any time, and Python will always keep track of its current value.
One must adhere to a few rules and guidelines when using variables in Python. Here are some of the rules-
• Variable names can contain only letters, numbers, and underscores. They can start with a letter or an underscore, but not with a number. For instance, you can call a variable var_1 but not 1_var.
• Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, first_name works, but first name will cause errors.
• Avoid using Python keywords and function names as variable names; that is, do not use words that Python like other languages has reserved for a particular programmatic purpose, such as the word print.
• Variable names should be short but descriptive. For example, email is better than em, first_name is better than f_n, and word_length is better than length_of_the_words.
• Be careful when using the lowercase letter l and the uppercase letter O because they could be confused with the numbers 1 and 0.
Python is dynamically-typed, meaning that no pre-declaration of a variable or its type is
necessary. The type (and value) are initialized on assignment. Assignments are performed
using the equals sign.
As an exercise you may try to create a few programs, say -
1. store a message in a variable and then print that message
2. Store a message in a variable, and print that message. Then change the value of your variable to a new message, and print the new message.
3.What if the first print statement is removed from the program? Just try and see the result.
We'll continue with this topic in the next blog.
0 comments:
Post a Comment