What does s mean in Python

Python course

Previous chapter: Blocks
Next chapter: Operators

Data types and variables

introduction

Even if you think you already know enough about variables and data types from other programming languages, you should at least skim this chapter, because some things are different in Python.

variables

A variable in the most general sense is simply a container for storing certain values, e.g. strings or numbers. In the course of the program, you can access these variables, or more precisely the value of their content, or assign a new value to them.
In most programming languages, such as C, it is the case that a variable designates a fixed memory location in which values ​​of a certain data type can be stored. The value of the variable can change during the program run, but the value changes must be of the same type. So you cannot have an integer stored in a variable at a certain point in time and then overwrite this value with a floating point number. The storage location of the variables is also constant during the entire run, so it can no longer be changed. In languages ​​like C, the storage location is fixed by the compiler.
This is different in Python. First of all, variables in Python do not denote a specific type and therefore you do not need a type declaration in Python either. For example, if you need a variable i with the value 42 in the program, you can do this simply with the following statement:

i = 42 The above statement should not be seen as a mathematical equal sign, but as "the value 42 is assigned to the variable i", ie the content of i is 42 after the assignment. You can also use this value of the variable, as you can see in the following example sees, then change:
>>> i = 42 >>> i = i + 1 >>> print i 43 >>>

numbers

Python knows four built-in data types for numbers:

  • Integer e.g. 4321
    preceding 0 means octal number and
    prefixed 0x means hex number
  • long integer
    They can be as long as you want
    They are denoted with an l at the beginning and L at the end.
  • Floating point numbers
    Numbers in the form 3.14159 or 17.3e + 02
  • complex numbers
    e.g. 1.2 + 3y

Strings

A string can be seen as a sequence of individual characters.



Every single character of a string can be addressed via an index. In the following example we see how the above string shown in the picture is defined in Python and how we can access it:

>>> s = "Python" >>> print s [0] P >>> print s [3] h The length of a string can be determined with the function len () and with this you can also simply refer to the last or Access the penultimate character of a string: >>> s = "Python" >>> index_last_char = len (s) - 1 >>> print s [index_last_char] n >>> print s [index_last_char - 1] o >>> Since there In practical work, it happens very often that you have to access individual characters of a string from the back, it would be very annoying if you always had to do this via the detour by calling the function len (). Python therefore offers a more elegant option. The indices are also numbered from the right by negative indices, i.e. the last character is addressed using the index -1, the penultimate using -2, etc. We see this illustrated in the following figure:



In the code it looks like this:
>>> s = "Python" >>> last_character = s [-1] >>> print last_character n

Strings can be created using
  • single quotation marks (')
    'This is a string with simple quotes'
  • double quotation marks (")
    "Mayers' dachshund is called Waldi"
  • triple quotation marks ('' ') or ("" ")
    '' 'String in triple quotation marks can span multiple lines and contain' single 'and' double 'quotation marks.' ''
can be specified.

Important string functions

A couple of string functions:
  • Concatenation (English: Concatenation)
    This function is used to connect two strings to a new string using the "+" operator:
    "Hello" + "World" -> "HelloWorld"
  • Repetition (English: repetition)
    A string can be concatenated repeatedly. The "*" operator is used for this.
    Example:
    "* - *" * 3 becomes "* - ** - ** - *"
  • Indexing
    "Python" [0] -> "P"
  • Slicing
    The English verb "to slice" means "to cut" or "to slice" in German. The latter meaning also corresponds to the function of slicing in Python. You cut a "slice" out of a string, so to speak. In the following expression, [2: 4] means that we cut out a substring from the string "Python", which begins with the character of index 2 (inclusive) and goes up to index 4 (exclusive):
    "Python" [2: 4] -> "th"

  • length of a string
    len ("Python") -> 6

Immutable strings

As in Java but not as in C or C ++, strings cannot be changed in Python. If you try to change an indexed position, you generate an error message: >>> s = "Some things are immutable!" >>> s [-1] = "." Traceback (most recent call last): File "", line 1, in TypeError: 'str' object does not support item assignment >>>

Escape or escape characters

There are strings that control the flow of text, such as a newline (line feed) or tab. They cannot be displayed on the screen as individual characters. Such characters are represented within string literals using special character strings, so-called escape sequences. An escape sequence is introduced by a backslash \, followed by the identifier of the desired special character. Overview of the escape characters:
  • \ Line continuation
  • \ backslash
  • \ 'Single quotation mark
  • \ "Double quotation mark
  • \ a bell
  • \ b regression
  • \ e Masking out
  • \ 0 zero
  • \ n Linefeed (LF)
  • \ v Vertical tab
  • \ t Horizontal tab
  • \ r carriage return (CR)
  • \ f form feed
  • \ 0XX Octal value
  • \ xXX Hexadecimal value
The evaluation of escape characters can be prevented by immediately preceding a string with an r or R.
Example:
r "\ n causes a line feed"

Type change for variables

In Python, as already said, a variable can be used immediately without declaring the data type. Nevertheless, Python assigns a data type, i.e. depending on the data type, the variable is created differently, i.e. as an integer, float, string, and so on. Actually, one should say that an object is created with a certain data type or class. The variable then references this object, i.e. the variable itself does not actually have a type. Expressed differently
The data type in Python is not tied to the variable but to the value, which implies that the type can change at runtime, as we can see in the following example:
i = 42 # data type is integer (implicit) i = 42 + 0.11 # type changes to float i = "fourty" # and now a string

Changing locations

In principle, in the previous case, although this is of course implementation-dependent, the storage location for the variable i will change. The interpreter can save the value as an integer with the instruction "i = 42", but must create a new location for a float number with the instruction "i = 42 + 0.11". For i = "fourty" it has to be converted into a string.
Caution: As a user, you don't actually need to know this, as everything happens automatically!

Let us now consider the following Python code: >>> x = 3 >>> y = x >>> y = 2
One would intuitively assume that Python first selects a storage location for x and stores the object (number) 3 there. Then the value of x is assigned to the variable y. In C and many other programming languages ​​there would also be a separate storage location for y, in which the number 3 would now be written. Python works differently: x is a variable with the object 3 and y is a variable with the "same" (not "same") object. x and y "point" to the same object. In the last line y is now assigned the value 2, a new object must now be created and y "points" to a new storage location. (Note: C programmers should not confuse this "show" used with the pointers used under C.)

The question now arises as to how one can check what has been said above. The identity function id () can be used for this. The identity of an instance is used to distinguish it from all other instances. The identity is an integer and it is unique within a program. The identity function id () returns the identity. In this way you can check whether it is a specific instance and not just one with the same value and type. We enter the above example again, but let us output the identity in each case: >>> x = 3 >>> print id (x) 157379912 >>> y = x >>> print id (y) 157379912 >>> y = 2 >>> print id (y) 157379924 >>> print id (x) 157379912 >>> We notice that the identity only changes after we have assigned a new value to y. The identity of x remains the same, i.e. the storage location of x is not changed.

Special features of strings

We can see a special effect with strings. We want to illustrate this in the following example. For this we also need the "is" operator. If a and b are two strings, then "a is b" checks whether a and b have the same identity (location). If "a is b" then "a == b" also applies. But if "a == b" holds, then of course "a is b" does not necessarily hold!
Now let's examine how strings are stored in Python. In the following example, we can see that a and b share the same storage location, although this time we have not used an assignment of the type "b = a": >>> a = "Linux" >>> b = "Linux" >> > a is b True But what if the string used is longer? In the following we use the longest place name in the world as a string. A community with a little more than 3000 inhabitants in the south of the island of Anglesey in the county of the same name in the north-west of Wales: >>> a = "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" >>> b = "Llanfairpwllgwyngyllgogerychwyrndogwogllochantys >> because what works for a municipality in Wales fails for example for Baden-Württemberg: >>> a = "Baden-Württemberg" >>> b = "Baden-Württemberg" >>> a is b False >>> a == b True So it cannot be due to the geographical location, as can be seen in the following example. Rather, it looks as if there shouldn't be any special characters or blanks in the string. >>> a = "Bathing!" >>> b = "Bathing!" >>> a is b False >>> a = "Baden1" >>> b = "Baden1" >>> a is b True

Previous chapter: Blocks
Next chapter: Operators