Chapter 1 ( Intro to Python
Chapter 1 ( Intro to Python
Why python?
Its easy to start with to jump in programming languages, and to learn the
other languages of computer science, you just need to know the logics of
programming languages, and most of the languages use the same logics. So
python makes it easier to learn about those logics. While if you start from
other languages, like traditionally from C or C++, its usually makes
understanding the logics complicated making the student, demotivational in
learning the programming languages.
All programs use basic instructions as building blocks. Here are few of
the most common ones, in English:
You write programming languages in a text editor ( called IDE) there are
many types of IDE, Like Spyder, Anaconda, or the most common, the Visual
Studio code, but these are too complex for a beginner to start with, so to
make it easier, we will gonna start with Mu editor, its one easy IDE for
Python, where the interface looks calm and easier. It is also supported on
Linux, Windows and Mac so availability isn’t a problem
IDE is for us to write or edit the code called Source Code, but its still is raw.
And computer don’t understand just source code, we all know that Computer
Only understands Binary Numbers. So for that , we use an interpreter or
compiler , that actually converts our code into Binary code so then our
machine, or computer understands it.
You can use plenty of other operators in Python expressions, too. For
example, Table below lists all the math operators in Python
In each case, you as the programmer must enter the expression, but
Python does the hard part of evaluating it down to a single value. Python will
keep evaluating parts of the expression until it becomes a single value, as
shown here
Then we have Integers, Floating-Point and string data types:
Integers: -2 , -1 , 0 , 1 , 2 , 3
String concatenation means that adding up two strings, its like ‘Alice’ +
‘Adams’ reduces down to ‘AliceAdams’ and String Replication means
Replicating a string like ‘Alice’ * 5 making it to ‘AliceAliceAliceAliceAlice’
Its to understand that although operators work great with the integers and
floating point numbers, but not all work well with strings, the only 2 operator
works with the strings are addition operator and multiplication operator with
specific rules. Like you can either add two strings or you can replicate a
string by multiplying it with an integer.
Variables:
Variables are like a box in computer’s memory, you assign or make variables
to store some values in it, it can be Boolean value, or String, or integer, or
Floating points.
Either you can store value while writing a code or you can just create an
empty variable and let the user to store any value in it, its like letting the
user to create a username, it will remain in the variable ( username = input()
for ever.
Assignment statements:
You’ll store values in variables with an assignment statement. An assignment
statement consists of a variable name, an equal sign ( called the assignment
operator ), and the value to be stored. If you enter the assignment
statement spam = 42, then a variable named spam will have the integer
value 42 stored in it.
Variables values can be updated, once you create a variable and input a
value , then create the variable again with the same name, and give a
different value, The value will change and new value will replace the older
value. Its called value over writing
We write our python programs in your File editor, the file editor is similar to
the text editors such as notepad or textmate, but it has some features
specifically for entering source code.
The file editor lets you type in many instructions, save the file, and run
the program. Here’s how you can tell the difference between the two:
The interactive shell window will always be the one with the prompt
>>>.
The file editor window will not have the prompt >>>.
Now its time to create your first program! When the file editor window opens,
Enter the following into it:
print('Hello, world!')
myName = input()
print(len(myName))
myAge = input()
Press the F5 key to run the program. The program output in the
interactive shell should look something like this:
Hello, world!
Uzair
21
When there are no more lines of code to execute, the python program
terminates; That is, it stops running. ( You can also say that Python program
exits.)
You can close the file editor by clicking the X at the top of the window.
To reload a saved program, select File > Open… from the menu. Do that
now, and in the window that appears, choose hello.py and click the Open
button. Your previously saved Hello.py program should open in the file editor
window.
With your new program open in the file editor, Lets take quick tour of
the python instructions it uses by looking at what each line of code does.
Comments:
Python ignores comments and you can use them to write notes or
remind your self what the code is trying to do. Any text for the rest of the line
following a hash mark (#) is part of a comment.
Python also ignores the blank line after the comment. You can add as
many blank lines to your program as you want.This can make your code
easier to read like a paragraph in a book.
The print() function displays the string values inside its parentheses on the
screen.
print('Hello, world!')
The line print(‘Hello World’) means “Print out the text in the string
‘Hello World!’”. When python executes this line, you say that python is
calling the print() function and the string value is being passed to the
function. A value that is passed to a function is called an argument. Notice
that the quotes are not printed to the screen. They just mark where the
string begins and ends; They are not part of the string value.
Note : You can also use this function to put a blank line on the screen; just
call print() with nothing in between the parentheses.
When you write a function name, the opening and closing parentheses
at the end identify it as the name of a function. We will learn more about
functions after the chapter of Flow control
The input() Function
The input function waits for the user to type sometext on the keyboard
and press Enter.
myName = input()
This function call evaluates to a string equal to the user’s text, and the
line of code assigns the myName variable to this string value.
The following call to print() actually contains the expression 'It is good
to meet you, ' + myName between the parentheses.
Len() Function
You can pass the len() function a string value ( or a variable evaluates
to the integer value of the number of characters containing a string ) and the
function in that string.
print(len(myName))
>>> len('hello')
46
>>> len('')
0
The print() function isn’t causing that error, but rather it’s the expression you
tried to pass to print(). You get the same error message if you type the
expression into the interactive shell on its own.
Python gives an error because the + operator can only be used to add two
integers together or concatenate two strings. You cant add an integer to a
string, because this is ungrammatical in python. You can fix this by using a
string version of the integer instead, as explained in the next section.
>>> str(29)
‘29’
>>> print('I am ' + str(29) + ' years old.'
I am 29 years old.
Because str(29) evaluates to ’29’, the expression 'I am ' + str(29) + '
years old.', Which in turn evaluates to 'I am 29 years old’. This is the value
that is passed to the print() function.
The str(), int(), and float() functinos will evaluate to the string, integer,
and floating-point forms of the value you pass, respectively. Try converting
some values in the interactive shell with these functions and watch what
happens.
>>> str(0)
'0'
>>> str(-3.14)
'-3.14'
>>> int('42')
42
>>> int('-99')-99
>>> int(1.25)
>>> int(1.99)
>>> float('3.14')
3.14
>>> float(10)
10.0
The previous examples call the str() , int() , and float() functions and
pass them values of the other data types to obtain a string, integer, or
floating-point form of those values.
The str() function is handy when you have an integer or float that you
want to concatenate to a string. The int() function is also helpful if you have
a number as a string value that you want to use in some mathematics. For
example, the input() function always returns a string, even if the user enters
a number. Enter spam = input() into the interactive shell and enter 101
when it waits for your text.
101
>>> spam
'101'
The value stored inside spam isn’t the integer 101 but the string ‘101’. If you
want to do math using the value in spam, use the int() function to get the
integer form of spam and then store this as the new value in spam.
>>> spam
101
202.0
>>> int('99.99')
int('99.99')
>>> int('twelve')
int('twelve')
>>> int(7.7)
>>>int(7.7)+1
You used the int() and str() functions in the last three lines of your
program to get a value of the appropriate data types for the code.
myAge = input()
The myAge variable contains the value returned from the input().
Because the input() function always returns a string ( even if the user typed
in a number ), you can use the int(myAge) code to return an integer value of
the string in myAge. This integer value is then added to 1 in the expression
int(myAge) + 1.
Lets say the user enters the string ‘4’ for myAge. The string ‘4’ is
converted to an integer, so you can add one to it. The result is 5. The str()
function coverts the result back to a string, so you can concatenate it with
the second string,’ in a year.’, so create the final message. These evaluation
steps would look something like the following:
Summary