What does s mean in Python
Next chapter: Operators
Data types and variables
introductionEven 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.
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 >>> i = i + 1 >>> print i 43 >>>
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
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:
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.' ''
Important string functionsA 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.
"* - *" * 3 becomes "* - ** - ** - *"
"Python"  -> "P"
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 stringsAs 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 "
Escape or escape charactersThere 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
r "\ n causes a line feed"
Type change for variablesIn 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 locationsIn 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 stringsWe 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
Next chapter: Operators
- What are some interesting psychological facts 2
- Why are the Gurkhas so feared
- When did Helen Keller die?
- How do conversations help develop character
- How to order checks at Walmart
- Are Turkish and Caucasian
- What should a woman know
- What is existentialism
- Can men make women pregnant when they are 60?
- What is the difference between rice and pasta
- Which can cause symptoms similar to ADD
- Why should you become a chef?
- What was your first impression of me
- What is your feedback on my website
- What luxury item should you never buy?
- How has influencer marketing affected your marketing?
- Is existence a form of quantum entanglement
- Can decrease the speed of light
- How are refugees treated in the USA
- Is pumpkin good for your kidneys
- What engines does Volvos have
- Is Survey Junkie real
- Why can't photons have a frame of reference
- American is a nationality