So, so far all the computation we've done
is operated only on numbers, and in the
early days of computing, people thought of computers
as super powerful calculators, for doing computations like
simulating nuclear weapons, computing ballistic tables, or breaking
encryptions, which was a little more than just
arithmetic. But still was mostly about counting and
doing simple arithmetic. We saw this quote from
Grace Hopper earlier talking about computers could only do arithmetic, and
this is what people thought about computers in the 1940s and
1950s. But there's no reason to limit computers to that. They
can operate on any kind of data we want, and it
gets much more interesting when we operate on data besides just
numbers. If we're going to build a search engine, most of the
data that we want to deal with is not numbers. It's
the letters that are contained in webpages, and in Python, that's what
we call a string. A string is just a sequence of
characters surrounded by quotes. So here's an example of a string
in Python, it starts with a single quote, has a sequence
of characters and anything that we can type on the keyboard can
be in a string, and ends with another single quote. The
string is the sequence of characters between the single quotes. If
we want, we can use double quotes instead. If we use
double quote, then the double quote starts the string. We can have
a sequence of characters and a double quote that ends the
string. The only requirement is that if we start the string
with a single quote, it has to end with a single
quote. If we start the string with a double quote, it
has to end with a double quote. And that's actually a
handy property. Because that means we can have the other kind
of quote within our string. This string starts with a double
quote. It contains a single quote inside it. But because we started
with a double quote, that single quote doesn't end the string.
That single quote is just like another character in the string.
The string continues. Until the closing double quote. So let's try
some things in the Python interpreter. So, we can print a
string, just like we can print a number. So here I'm
printing the string hello. And when we run this, we see
the output hello. It's printing hello. We don't see the single
quotes as it prints, but we know that it's a string that
was printed. We can print a string with double quotes,
and one thing to notice when you enter strings through the
interpreter, the color is black now, since it's an open
string, it hasn't been finished, once I type the final quote,
that closes the string, the color changes to blue. So
now when we run this, we have two prints, both that
print hello, it looks the same both times. It doesn't matter
if we use single quotes or double quotes around our string.
Just to check everyone's paying attention, I'm going to try one
more thing. And now I am printing hello without the quotes.
You can guess what that will do. We won't make a
quiz of this, but try to guess before I run it.
Now that I run this, we see the result.
We get a name error. The name Hello is not
defined. Without the quotes, this looks just like a variable.
It's a name, but it's a variable that we didn't
define. So when I try to use it, I get
an error that the variable name Hello is not defined.
If I wanted to I could define a variable named
hello. Let's make hello refer to the string howdy and
now when I print hello, it works. I see the first
two prints that printed hello, now when I print the variable hello,
well that refers to the string howdy and I see howdy as
the result. We usually don't want our variables to start with capital
letters. That's just a convention, so I'm going to change this
back to a lower case hello because it makes me feel uncomfortable
to have a variable with a capital letter. There's no rule against
that in Python. It's just a convention that we like to follow.
ここまで私たちの行ってきた計算はすべて
数値だけで処理されました
コンピュータが利用され始めた頃は
コンピュータは
計算を行う非常に強力な計算機だと思われていました
例えば核兵器のシミュレーション、
弾道表の計算、暗号の解読などです
これは単純な計算だけではありませんが
主に数を数えたり簡単な計算をしたりするものでした
前に話したホッパー氏の言葉にも
コンピュータができるのは計算だけ
というものがありました
それが1940~50年代に人々が持っていた
コンピュータのイメージです
しかしコンピュータの用途を
そのように限定する理由はありません
コンピュータはあらゆる種類のデータを処理できます
数値以外のデータも処理できれば
もっと興味深い結果が得られるでしょう
検索エンジンを作成する場合は
取り扱いたいデータの大部分は数値ではなく
Webページに含まれる文字や
Pythonにおいて文字列と呼ばれるものです
文字列は
クォーテーションに囲まれた連続した文字です
これがPythonの文字列の例です
文字列はシングルクォーテーションから始まります
キーボードで入力できるものは何でも文字列になります
もう1つのシングルクォーテーションで
文字列が終わります
文字列は
シングルクォーテーションに挟まれた一連の文字です
代わりにダブルクォーテーションを使うこともできます
その場合は
文字列はダブルクォーテーションから始まり
一連の文字の最後にダブルクォーテーションをつけます
唯一の要件は
シングルクォーテーションで文字列を始めたら
シングルクォーテーションで終わらせ
ダブルクォーテーションで文字列を始めたら
ダブルクォーテーションで終わらせることです
この仕組みはとても便利です
文字列の中に違う種類のクォーテーションを
含む場合があるからです
この文字列はダブルクォーテーションで始まります
文字列内にシングルクォーテーションがありますが
ダブルクォーテーションから始めたので
このシングルクォーテーションがあっても
文字列は終わりません
このシングルクォーテーションは
文字列内の文字と同様になります
ダブルクォーテーションで終了するまで
文字列は続きます
Pythonインタプリタでいくつか試してみましょう
文字列は数値と同様に表示できます
文字列Helloを表示してみます
これを実行するとHelloと表示されます
シングルクォーテーションは表示されません
文字列が表示されます
ダブルクォーテーションを使って
文字列を表示することもできます
インタプリタで文字列を入力する際に
注目すべきことがあります
この文字の色が今は黒いのは
文字列がまだ終わっていないからです
最後のクォーテーションを入力すると
文字列が終了して青色に変わります
これを実行するとHelloと2回表示されます
どちらも同じに見えます
文字列を囲むクォーテーションは
シングルでもダブルでも構いません
理解していただけたかを確かめるため
もう1つ試してみます
クォーテーションをつけずにHelloを表示します
どうなると思いますか?
これを小テストにはしませんが
実行する前に考えてみてください
それでは実行して結果を確認しましょう
名前Helloが定義されていませんという
NameErrorが出ます
クォーテーションがないと
これはただの変数のようになります
定義されていない変数名です
したがって その名前を使おうとすると
変数名Helloが定義されていないというエラーが
出るのです
必要ならHelloという名前の変数を定義できます
Helloが文字列Howdyを参照するようにしましょう
今回はprint Helloを正しく実行できます
最初にHelloと2回表示しています
そして変数Helloを表示すると
これは文字列Howdyを参照しているので
Howdyと表示されます
通常は変数の最初の文字を大文字にしません
ただの慣習なのですが
これを小文字のhelloに変えます
大文字になっている変数があると
私は違和感を覚えるからです
Pythonのルールで禁止されているわけではなく
単に好まれている慣習です
Şimdiye kadar yaptığımız bütün
hesaplamaları rakamlar üzerinde
gerçekleştirdik. Bilgisayarın ilk
zamanlarında insanlar
bilgisayarları nükleer silah simülasyonu
yapan, balistik tablolar hesaplayan
yada gizli şifreleri çözen bir nevi süper
hesap makinası olarak görüyorlardı.
Fonksiyonları normal
aritmetik işlemlerin bir tık ötesi
olsa da eninde sonunda çoğu hesaplama ve
basit aritmatik
yapan makinalardı. Grace Hopper'ın
bilgisayarların o
dönemde nasıl görüldüğüne dair sarf etmiş
olduğu sözü daha önce paylaşmıştık.
1940 ve 1950'lerde insanlar bilgisayar
hakkında böyle düşünüyorlardı
Ancak bilgisayarı bu şekilde kısıtlamak
için geçerli bir sebep yok
Bilgisayar her türlü veri üzerinde işlem
yapabilir hatta
rakamlar dışında yaptığı işlemler aslında
daha ilginçtir
Bir arama motoru tasarlayacaksak üzerinde
çalışacağımız verinin
çoğu rakamlardan oluşmayacak. Web
sayfalarını
dolduran harfler ve karakterler olacak.
Python'da biz bu tür verilere karakter
dizisi diyoruz. Bir karakter dizisi tırnak
işareti
içine alınan bir dizi karakterden oluşur.
Bir örnek görelim:
Tek tırnak işareti, sonra bir karakter
dizisi, ki bu
klavyedeki herhangi bir şey
olabilir ve sonra
tekrar bir tek tırnak
işareti. Buradaki karakter dizimiz
bu tek tırnak işaretlerinin arasındaki
kısımdır. Tek tırnak yerine istersek çift
tırnak da kullanabiliriz. Çift tırnak
kullanacaksak başta
onunla başlamalı ve sonrasında karakter
dizisini yazmalı ve
sonunda yine çift tırnak
ile bitirmeliyiz.
Buradaki tek kural eğer tek tırnak ile
başlanılmışsa
tek ile çift ile başlanmışsa çift tırnak
le bitirmektir.
Bu esasında kullanım kolaylığı da
sağlayan bir özellik zira hangisi ile
başlamışsak diğerini karakter dizisi
olarak kullanmak mümkün.
karakter dizisi çift tırnak ile başladığı
için karakterlerin
içinde tek tırnak karakterini kullanmam
mümkün oluyor.
Tek karakterle başlamadığım için
karakterin
içindeki tek tırnak karakter dizisini
sonlandırmıyor.Burada
tek tırnak karakteri diğer karakterler
gibi herhangi bir karakterdir
Şimdi Python'da birkaç örneğe bakalım.
Bir karakter dizisini bir rakam gibi
yazdırabiliriz. Burada "merhaba" karakter
dizisini yazdırıyorum.
görebiliyorsunuz. "Merhaba"yı yazdırdı.
Tek tırnakları görmüyoruz.
Zira karakter dizisini tanımlıyorlar.
Çift tırnakları kullanalım.
"Hello yazarken henüz karakterler
sonlanmadığından Python
bunu siyah olarak gösteriyor, tırnağı
kapattığımda ise
artık karakter dizisi sonlandığından mavi
olarak renklendiriyor. Şimdi bunu
yazdıralım.
Gördüğünüz gibi iki adet aynı "merhaba"
karakter dizim oldu.
Tek yada çift tırnak kullanmam kullanımı
değiştirmedi. Aynı sonucu verdi.
Size önemli bir şey göstermek için
son bir şey daha deneyeceğim. Hello'yu
etrafında tırnak işaretleri
yokken
yazdırmaya çalışacağım. Ne olacağını
tahmin edebilirsiniz.
Bu bir test sorusu değil, ancak lütfen
tahmin etmeye çalışın.
Evet, çalıştırdığımda sonucu görüyorsunuz,
hata verdi.
bize "Hello"nun tanımlanmadığını söylüyor.
Tırnak işaretleri olmadan bu bir
değişkene benziyor. Değişken ise ancak
önceden tanımlandığında
kullanılabilir, aksi halde Python
hata verir.
Bu yüzden değişken tanımlama
hatası alıyorum.
Şimdi "Howdy" isimli karakter dizisini
Hello isimli bir değişkene atayalım. Şimdi
bunu yazdırdığımda artık hata yok ve
Python problemsiz yazdırdı.
Gördüğünüz gibi önce iki adet "Hello"
karakter dizilerimizi
sonra ise hello değişkeninin değeri
olan "howdy"'yi
ekrana yazdırdık. Değişkenlerimizin ilk
harflerinin büyük olmasını genelde tercih
etmeyiz. Şimdi onu küçük hale
getiriyorum. Bu bir yanlış değil,
sadece bir tavsiye ve gelenektir.
Bu şekilde daha rahat ediyorum.
Diğeri beni rahatsız ediyor.
Python'da yasaklı değil
sadece yıllar yılı Python'da
yerleşmiş bir gelenektir.
迄今为止 我们主要是运用计算机
对数字进行计算
在计算机发展初期
人们就将计算机看作是强有力的超级计算器
用来完成一些计算类工作
比如模拟核武器 计算弹道表 或者破解加密 只是多了一些算法
但是基本上都集中在计算和简单的算术领域
这个引用来自于 Grace Hopper
早期他认为计算机只能做算术工作
这也是20世纪40及50年代 人们对计算机的普遍看法
但是我们没有理由将计算机仅限于此
它们可以处理任何我们想要处理的数据
当计算机能够处理除数字以外的数据时 它将变得更加有趣
如果我们要创建一个搜索引擎
我们要处理的大部分数据不是数字
而是网页中包含的字母 在 Python 中
我们称之为字符串 字符串就是被引号包围的一串字符
这是一个 Python 字符串的例子
从一个单引号开始
然后有一串字符 用键盘能输入的任何字符都可以作为字符串
以另一个单引号结束
字符串就是在这个单引号之间的一串字符
如果愿意 我们也可以使用双引号来替代
如果我们使用双引号 那就用双引号来作为这个字符串的开始
这里是一串字符 然后以另一个双引号结束
唯一的要求就是 如果我们用单引号作为字符串的开始
那么结尾处也要使用单引号
如果我们使用双引号作为开始
那么结尾处也要使用双引号
这其实是个很灵活的属性 因为这就意味着我们可以在字符串中使用另一种引号
这个字符串以双引号开始
里面包含了一个单引号 但是因为是以双引号开始的
所以这个单引号不能够结束这个字符串
这个单引号就相当于字符串中的其它字符
字符串继续 直到用来结束的双引号
我们用 Python 解释器来试一下
我们输入一个字符串 就像输入数字一样
这里我输入字符串 hello 当我运行这个字符串的时候
我们可以看到解释器输出 hello
在输出结果中 我们不会看到单引号
但是我们知道这是一个输出的字符串 我们也可以输入一个带双引号的字符串
需要注意的是当你在解释器中输入字符串的时候
它的颜色是黑的
因为这是一个开放字符串 还没有完成 一旦我输入结束引号
这个字符串就闭合了 颜色就会变成蓝色
所以当我们运行这个字符串时 我们就得到两个输出结果
输出结果均为 hello 两次的结果显示是一样的
所以使用单引号和双引号都是可以的
但是为了强调 我要尝试另一件事
现在我输入不加引号的 hello
你可以预测出结果 这不是一个测验
但是在我运行它之前你可以试着猜一下结果
现在我来运行它 我们可以看到结果
我们得到一个 name error 的显示 Hello 没有被定义
如果不加引号 这个看起来像是一个变量
这是一个名称 但是是一个我们没有定义的变量
所以当我尝试运行它的时候
就会出现错误 并指出变量名称 Hello 没有被定义
如果我愿意的话 我可以定义一个名叫 hello 的变量
现在让 hello 指代字符串 howby
现在当我输入 hello 时 它就可以运行
前两个字符串输出的结果是 hello 现在当我输入变量 hello 时
它就指向字符串 howby 然后输出结果就是 howby
通常我们不会让变量以大写字母开始
这只是一个传统 所以我要把这个 Hello 改回到小写的 hello
因为变量中有大写字母看起来很不舒服
Python 里没有明确的规定禁止使用大写字母
这只是我们喜欢遵循的一个传统