+General

Starting Python

Starting python can be done by typing "python" in console:

pi@raspberrypi:~ $ python
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

This will bring up the python shell, where python commands can be tested, but this is not good for writing programs. For writing programs in terminal, or over ssh you can use an editor. After the written file is saved (as ...py) it can be run, with the word "python" in front of the file name:

pi@raspberrypi:~ $ cat hello.py
print ('Hello')
print ('World')

pi@raspberrypi:~ $ python hello.py
Hello
World

(version 2 is just called Python, whereas version 3 is called Python 3. If you want to run files with Python 3, put python3 in front of the file name.)

==================================

Indent

When you’re starting out in Python, it’s not uncommon to see an error such as IndentationError: unexpected indent, which means that somewhere things are not indented correctly. In Python spaces (indents) are important, and Python treats Tab characters differently.

==================================

Special characters, comparison

                                <--comment out a line
+, -, *, /                        <--arithmetic operators
and, or, not                      <--logical operators

x = 17
if x >= 10 and x <= 20:
   print("x is between 10 and 20")


Comparison:
                                 <--Less than
>                                  <--Greater than
<=                                 <--Less than or equal to
>=                                 <--Greater than or equal to
==                                 <--Exactly equal to
!=                                 <--Not equal to

You can compare strings as well:
"aa" < "ab"                        <--True
"aaa" < "aa"                       <--False

\'                                 <--displays a single quote in output
\"                                 <--displays a double quote in output
\\                                 <--displays a single backslash in output
\n                                 <--inserts a new line into the output
\t                                 <--inserts a horizontal tab into the output


==================================

Variables


In Python, you don’t have to declare the type of a variable, you can just assign a value:

a = 123
total = 12.34
c = "Hello Bello"
d = True
number_of_characters = 123
s1="abc"
s2="def"
s=s1+s2

You can define character string constants using either single or double quotes. The logical constants in Python are True and False and are case-sensitive. By convention, variable names begin with a lowercase letter and if the variable name consists of more than one word, the words are joined together with an underscore character.

You can determine what data type Python has assigned to a variable by using the type function, like: print (type(s1))   

=====================================

print ()

To print characters (string), it needs to be enclosed in singe quotes (') or double quotes ("). It does not matter which one. If you need to print a string containing double quotes, then you start with single quotes, like this:
print ('This is a quote:"Ohh".')


#reads in input from user to the give variable (raw_input() is in Python2, in python3 it is : input())
(the variable will be string, if you need number it needs to be converted)
x=raw_input('Enter value:')
print (x)

=====================================

No comments:

Post a Comment