(→Very simple Python) |
(→Very simple Python) |
||

Line 7: | Line 7: | ||

Type | Type | ||

− | x=1 | + | >>>x=1 |

and then simply | and then simply | ||

− | x | + | >>>x |

and you'll see | and you'll see | ||

Line 20: | Line 20: | ||

Type | Type | ||

− | x=1.01 | + | >>>x=1.01 |

and then after you type "x" you'll see | and then after you type "x" you'll see | ||

− | x | + | >>>x |

1.01 | 1.01 | ||

Line 30: | Line 30: | ||

Clearly you have a real-time calculator in hand, so try something more exciting. | Clearly you have a real-time calculator in hand, so try something more exciting. | ||

− | x=1.01 | + | >>>x=1.01 |

− | y=1.0001 | + | >>>y=1.0001 |

x/y | x/y | ||

Line 40: | Line 40: | ||

Modify that with | Modify that with | ||

− | z=x/y | + | >>>z=x/y |

− | z | + | >>>z |

and you'll see the same result. But now try | and you'll see the same result. But now try | ||

− | int(z) | + | >>>int(z) |

and you'll see | and you'll see | ||

1 | 1 | ||

+ | |||

+ | |||

+ | That is, the function int() took the integer part of z. You can put that in another variable such as | ||

+ | |||

+ | >>>a=int(z) | ||

+ | >>>a | ||

+ | 1 | ||

+ | |||

+ | Curiously, a seems to be an integer. It is said to be ''dymanically typed'' in this assignment. That can change. If you now add a little bit to a you'll see it turns into a floating point number | ||

+ | |||

+ | >>>a = a + 0.001 | ||

+ | >>>a | ||

+ | 1.001 |

This page contains examples and links to programs used for our Research Methods - Programming with Python short course.

Start a Python interactive session using the "python" command to get a >>> prompt.

Type

>>>x=1

and then simply

>>>x

and you'll see

1

Type

>>>x=1.01

and then after you type "x" you'll see

>>>x 1.01

Clearly you have a real-time calculator in hand, so try something more exciting.

>>>x=1.01 >>>y=1.0001 x/y

and you'll see something like this

1.0098990100989902

Modify that with

>>>z=x/y >>>z

and you'll see the same result. But now try

>>>int(z)

and you'll see

1

That is, the function int() took the integer part of z. You can put that in another variable such as

>>>a=int(z) >>>a 1

Curiously, a seems to be an integer. It is said to be *dymanically typed* in this assignment. That can change. If you now add a little bit to a you'll see it turns into a floating point number

>>>a = a + 0.001 >>>a 1.001