Difference between revisions of "Python quick reference"

From thelinuxwiki
Jump to: navigation, search
(simple string assignment)
(simple string assignment)
Line 12: Line 12:
 
  >>> var1 = 'foo'
 
  >>> var1 = 'foo'
 
</source>
 
</source>
 +
 +
this automatically creates a variable of type string
 +
 
  >>> type(var1)
 
  >>> type(var1)
 
  <type 'str'>
 
  <type 'str'>
 +
 +
printing your variable...
 +
 
<source lang="python">
 
<source lang="python">
 
  >>> print var1
 
  >>> print var1
Line 27: Line 33:
 
  File "<stdin>", line 1, in ?
 
  File "<stdin>", line 1, in ?
 
  NameError: name 'foo' is not defined
 
  NameError: name 'foo' is not defined
 +
 +
====integar variable====
 +
 +
<source lang="python">
 +
>>> my_int_var1 = 1
 +
>>> print my_int_var1
 +
1
 +
</source>
  
 
===print===
 
===print===

Revision as of 15:58, 14 August 2017

Contents

Introduction

Lexical analysis

Data model

Execution model

Expressions

Simple statements

variable operations

assigenment examples

simple string assignment

 >>> var1 = 'foo'

this automatically creates a variable of type string

>>> type(var1)
<type 'str'>

printing your variable...

 >>> print var1
 foo

string assignment like above must be incapsulated by quotes or the right side is interpretted as a variable name.

example:

>>> var1 = foo
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'foo' is not defined

integar variable

>>> my_int_var1 = 1
>>> print my_int_var1
1

print

print sing variable named foo

>>> print foo

print multiple variables with text

print 'my variable are %s %s' % (FOO, BAR)

Common string operations

print nth word of string

 print s.split()[n]